#include <bits/stdc++.h>

using namespace std;
using ll=long long;
using ld=long double;
using ull=unsigned long long;
using lll=__int128;

#ifdef LOCAL
#define deb(x) cout<<__LINE__<<": "<<(#x)<<" = "<<(x)<<endl;
#else
#define deb(x)
#endif
#define all(x) x.begin(), x.end()
#define arr(x) x.rbegin(), x.rend()
#define int ll
constexpr int MOD=998244353;
int pw(int x, int y) {
    if (y==0) return 1;
    if (y%2==0) return pw((x*x)%MOD, y/2);
    return (x*pw(x,y-1))%MOD;
}
signed main(){
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n,k;
    cin>>n>>k;
    vector<int> p(n);
    for (auto i=0; i<n; ++i) cin>>p[i];
    vector<int> ans(8);
    for (auto j=0; j<n; ++j) {
        ans[0]+=pw(p[j],j+1);
        ans[1]+=pw(p[j],n-j);
        ans[2]+=pw(n-p[j]+1,j+1);
        ans[3]+=pw(n-p[j]+1,n-j);
    }
    for (auto j=0; j<n; ++j) {
        ans[4]+=pw(j+1,p[j]);
        ans[5]+=pw(n-j,p[j]);
        ans[6]+=pw(j+1,n-p[j]+1);
        ans[7]+=pw(n-j,n-p[j]+1);
    }
    for (auto i=0; i<8; ++i) {
        ans[i]%=MOD;
    }
    int x=0,y=0,z=0;
    while (k--) {
        int g;
        cin>>g;
        if (g==3) {
            x^=1;
            swap(y,z);
        }else if (g==2) y^=1;
        else z^=1;
        cout<<ans[z+2*y+4*x]<<' ';
    }
}
