#include <bits/stdc++.h>
#define endl "\n"

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

long long n,Q;
long long p[MAXN],b[MAXN],org[MAXN];
long long pom[MAXN];

long long 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(long long c)
{
    if(c == 1)
    {
        std::reverse(p+1,p+n+1);
    }
    if(c == 2)
    {
        for(long long i = 1; i <= n; i++) p[i] = (n+1) - p[i];
    }
    if(c == 3)
    {
        for(long long i = 1; i <= n; i++) pom[p[i]] = i;
        for(long long i = 1; i <= n; i++) p[i] = pom[i];
    }

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

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


        long long c2 = solve(2);
        for(long long i = 0; i < 8; i++) if(v[i] == c2) mat[q][2] = i;
        c2 = solve(2);

        long long c3 = solve(3);
        for(long long i = 0; i < 8; i++) if(v[i] == c3) mat[q][3] = i;
        c3 = solve(3);
    }

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

int main()
{
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
    read();
    return 0;
}
