#include<bits/stdc++.h>
using namespace std;


#define int long

const int MOD = 998244353;

int bin_pow(int a, int n){
    if(n == 0) return 1;
    if(n == 1) return a;
    int tmp = bin_pow(a, n / 2);
    tmp = (tmp * tmp) % MOD;
    if(n % 2) tmp = (tmp * a) % MOD;
    return tmp;
}

int precalc[8];

map<int, int> code;

vector<int> go[8];


signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    go[0] = {1, 2, 4};
    go[4] = {6, 5, 0};
    go[2] = {3, 0, 6};
    go[6] = {4, 7, 2};
    go[1] = {0, 3, 5};
    go[5] = {7, 4, 1};
    go[3] = {2, 1, 7};
    go[7] = {5, 6, 3};
    int n, q;
    cin >> n >> q;
    vector<int> a(n);
    for(int i = 0; i < n; i++) cin >> a[i];
    vector<int> sv = a;
    for(int i = 0; i < 2; i++){
        for(int j = 0; j < 2; j++){
            for(int k = 0; k < 2; k++){
                vector<int> a = sv;
                if(i) reverse(a.begin(), a.end());
                if(j) for(int i = 0; i < n; i++) a[i] = n + 1 - a[i];
                if(k){
                    vector<int> nw(n);
                    for(int i = 0; i < n; i++) nw[a[i] - 1] = i + 1;
                    a = nw;
                }
                int res = 0;
                for(int i = 0; i < n; i++) res = (res + bin_pow(a[i], i + 1)) % MOD;
                //precalc[i][j][k] = res;
                //cout << i + j * 2 + k * 4 << " " << res << "\n";
                precalc[i + j * 2 + k * 4] = res;
                //cout << res << "\n";
                //cout << i << " " << j << " " << k << " " << res << "\n";
            }
        }
    }
    
    int nid = 0;
    while (q--)
    {
        int type;
        cin >> type;
        type--;
        //cout << nid << " " << go[nid][type] << "\n";
        nid = go[nid][type];
        
        cout << precalc[nid] << " ";
    }
    
}
/*
5 3
5 3 1 4 2
3 1 2
293 893 317

*/