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

const ll mod=998244353;

ll pw(ll x, ll exp) {
    if (exp==0) {
        return 1;
    }
    if (exp&1) {
        return (x*pw(x,exp-1))%mod;
    }
    ll z=pw(x,exp/2);
    return (z*z)%mod;
}

void fun(vector<ll>&perm,ll type) {
    ll i,n=perm.size();
    if (type==1) {
        reverse(perm.begin(),perm.end());
        return;
    }
    if (type==2) {
        for (i=0;i<n;i++) {
            perm[i]=n+1-perm[i];
        }
        return;
    }
    vector<ll>inv(n);
    for (i=0;i<n;i++) {
        inv[perm[i]-1]=i+1;
    }
    for (i=0;i<n;i++) {
        perm[i]=inv[i];
    }
}

int main() {
    ll i,j,n,k,t,q;
    cin>>n>>q;
    vector<vector<ll>>perms(16,vector<ll>(n));
    for (i=0;i<n;i++) {
        cin>>perms[0][i];
        for (j=1;j<16;j++) {
            perms[j][i]=perms[0][i];
        }
    }
    vector<vector<ll>>oper(16);
    oper[1]={1,2,3};
    oper[2]={1,3,2};
    oper[3]={2,1,3};
    oper[4]={2,3,1};
    oper[5]={3,1,2};
    oper[6]={3,2,1};
    oper[7]={1,2};
    oper[8]={1,3};
    oper[9]={2,1};
    oper[10]={2,3};
    oper[11]={3,1};
    oper[12]={3,2};
    oper[13]={1};
    oper[14]={2};
    oper[15]={3};
    for (i=0;i<16;i++) {
        for (auto o:oper[i]) {
            fun(perms[i],o);
        }
    }
    map<vector<ll>,ll>mp;
    for (i=0;i<16;i++) {
        mp[oper[i]]=i;
    }
    vector<ll>sc(16);
    for (i=0;i<16;i++) {
        for (j=0;j<n;j++) {
            sc[i]+=pw(perms[i][j],j+1);
        }
        sc[i]%=mod;
    }
    vector<ll>guys;
    while (q--) {
        cin>>k;
        bool met=false;
        for (auto o:guys) {
            if (o==k) {
                met=true;
            }
        }
        if (met) {
            vector<ll>nw;
            for (auto o:guys) {
                if (o!=k) {
                    nw.push_back(o);
                }
            }
            guys=nw;
            if (true) {
                reverse(guys.begin(),guys.end());
            }
        }
        else {
            guys.push_back(k);
        }
        cout<<sc[mp[guys]]<<" ";

    }
    cout<<'\n';
    return 0;
}