#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],org[MAXN];
int pom[MAXN];

int mat[8][4];
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;
}
/*
5 20
1 4 2 5 3
1 3 2 1 2 3 1 3 2 1 3 2 1 1 2 1 3 2 1 2
*/
long long solve(int c)
{
    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];
    }

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

    vector<int>h = {0,1,3,2,1,2,3,1};
    vector<int>v;
    v.resize(8);
    for(int i = 0; i < 8; i++) v[i] = solve(h[i]);
    //for(int i = 0; i < 8; i++) cout << v[i] << endl;
    for(int q = 0; q < 8; q++)
    {
        for(int i = 1; i <= n; i++) p[i] = org[i];
        for(int i = 0; i <= q; i++) int c = solve(h[i]);
        int c1 = solve(1);
        for(int i = 0; i < 8; i++) if(v[i] == c1) mat[q][1] = i;

        for(int i = 1; i <= n; i++) p[i] = org[i];
        for(int i = 0; i <= q; i++) int c = solve(h[i]);
        int c2 = solve(2);
        for(int i = 0; i < 8; i++) if(v[i] == c2) mat[q][2] = i;

        for(int i = 1; i <= n; i++) p[i] = org[i];
        for(int i = 0; i <= q; i++) int c = solve(h[i]);
        int c3 = solve(3);
        for(int i = 0; i < 8; i++) if(v[i] == c3) mat[q][3] = i;
    }

    for(int i = 1; i <= n; i++) p[i] = org[i];
    int last = 0;
    for(int i = 1; i <= Q; i++)
    {
        last = mat[last][b[i]];
        cout << v[last] << " ";
    }
    cout << endl;
}

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