#include <bits/stdc++.h>
using namespace std;
#define int long long
void solve();

signed main() {
#ifdef openol
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
#else
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
#endif
    int t=1;
    //cin>>t;
    while (t--) solve();
}

vector<int> rev(vector<int> &a) {
    int n=a.size()-1;
    vector<int> ans(n+1);
    for (int i=1;i<=n;i++) ans[n+1-i]=a[i];
    return ans;
}
vector<int> flip(vector<int> &a) {
    int n=a.size()-1;
    vector<int> ans(n+1);
    for (int i=1;i<=n;i++) ans[i]=n+1-a[i];
    return ans;
}

vector<int> f(vector<int> &a) {
    int n=a.size()-1;
    vector<int> ans(n+1);
    for (int i=1;i<=n;i++) ans[a[i]]=i;
    return ans;
}

int mod = 998244353;

int bp(int a,int x) {
    if (x==0) return 1;
    if (x%2==0) {
        int res=bp(a,x/2);
        return (res*res)%mod;
    }
    int res=bp(a,x/2);
    res*=res;
    res%=mod;
    res*=a;
    return res%mod;
}

int cost(vector<int> a) {
    int ans=0;
    int n=a.size()-1;
    for (int i=1;i<=n;i++) ans+=bp(a[i],i);
    ans%=mod;
    return ans;
}

vector<int> compr(vector<int> a) {
    vector<int> ans;
    for (auto i:a) {
        if (ans.empty() || ans.back()!=i) ans.push_back(i);
    }
    return ans;
}

int slv(vector<int> cur,vector<int> a) {
    for (auto i:cur) {
        if (i==1) a=rev(a);
        if (i==2) a=flip(a);
        if (i==3) a=f(a);
    }
    return cost(a);
}

void solve(){
    int n,q;
    cin>>n>>q;
    vector<int> cur={};
    vector<int> a(n+1);
    for (int i=1;i<=n;i++) {
        cin>>a[i];
    }
    // while (q--) {
    //     int b;
    //     cin>>b;
    //     if (b==1) a=rev(a);
    //     if (b==2) a=flip(a);
    //     if (b==3) a=f(a);
    //     cout<<cost(a)<<" ";
    // }
    // return;
    map<vector<int>,int> mp;
    for (int qwq=0;qwq<q;qwq++) {
        int b;
        cin>>b;
        if (b==3) {
            int cnt_3=0;
            for (auto i:cur) cnt_3+=(i==3);
            assert(cnt_3<=1);
            if (cnt_3==1) {
                if (cur.back()==3) {
                    cur.pop_back();
                }
                else if (cur[cur.size()-2]==3){
                    int v=cur.back();
                    cur.pop_back();
                    cur.pop_back();
                    cur.push_back(3-v);
                }
                else {
                    int v1=cur.back();
                    cur.pop_back();
                    int v2=cur.back();
                    cur.pop_back();
                    cur.pop_back();
                    cur.push_back(v1);
                    cur.push_back(v2);
                }
            }
            else cur.push_back(3);
        }
        else {
            cur.push_back(b);
            vector<int> af;
            for (int i=(int)cur.size()-1;i>=0;i--) {
                if (cur[i]==3) break;
                af.push_back(cur[i]);
                cur.pop_back();
            }
            sort(af.begin(),af.end());
            af=compr(af);
            for (auto i:af) cur.push_back(i);

        }
        if (mp.find(cur)==mp.end()) {
            mp[cur]=slv(cur,a);
        }
        assert(cur.size()<=5);
        cout<<mp[cur]<<' ';
    }
}