#include <bits/stdc++.h>

using namespace std;

const long long mod = 998244353;
const int MAXN = 1e5+5;

int n,q;
int p[MAXN],b[MAXN];
int pom[MAXN];
long long ans[2];

long long step(long long a, long long b)
{
    long long res = 1;
    while(b > 0)
    {
        if(b % 2 == 1) res = (res*a)%mod;
        b /= 2;
        a = (a*a)%mod;
    }
    return res;
}
void solve(int c)
{
    long long res = 0;
    for(int i = 1; i <= n; i++) res = (res + step(p[i],i))%mod;
    ans[0] = res;

    if(c == 1)
    {
        reverse(p+1,p+n+1);
    }
    if(c == 2)
    {
        for(int i = 1; i <= n; i++) p[i] = (n+1) - p[i];
    }
    if(c == 3)
    {
        for(int i = 1; i <= n; i++) pom[p[i]] = i;
        for(int i = 1; i <= n; i++) p[i] = pom[i];
    }

    res = 0;
    for(int i = 1; i <= n; i++) res = (res + step(p[i],i))%mod;
    ans[1] = res;
}
void read()
{
    cin >> n >> q;
    for(int i = 1; i <= n; i++) cin >> p[i];
    for(int i = 1; i <= q; i++) cin >> b[i];

    solve(b[1]);
    for(int i = 1; i <= q; i++) cout << ans[i%2] << " ";
    cout << endl;
}

int main()
{
    read();
    return 0;
}
