#include <bits/stdc++.h>

using namespace std;

void solve();

// #include<ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
// using ordered_set = tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>;

int main() {
#ifdef LOCAL
    freopen("input.txt","r",stdin);
#endif
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t=1;
    //cin>>t;
    while (t--) {
        solve();
    }
    return 0;
}

#define int long long
#define endl '\n'
const int N=3e5+100,MOD=998244353,INF=2e18;
const double EPS=1e-6,pi=acos(-1);
int mul(int a,int b) {
    return (a*b)%MOD;
}
int add(int a,int b) {
    if (a+b>=MOD)return a+b-MOD;
    return a+b;
}
int binpow(int n,int m) {
    if (m==0)return 1;
    int v=binpow(n,m/2);
    v=mul(v,v);
    if (m&1)v=mul(v,n);
    return v;
}

int find(vector<int> &a) {
    int ans=0,n=a.size();
    for (int i=0;i<n;i++) {
        ans=add(ans,binpow(a[i],i+1));
    }
    return ans;
}
void solve() {
    int n,m;
    cin>>n>>m;
    vector<int> p(n);
    for (int i=0;i<n;i++) {
        cin>>p[i];
    }
    vector<int> a(m);
    for (int i=0;i<m;i++) {
        cin>>a[i];
    }

    int start=0;
    if (a[0]==3) {
        bool fl=false;
        for (int i=0;i<m;i++) {
            if (a[i]!=3) {
                start=i;
                fl=true;
                break;
            }
        }
        if (!fl)start=n;
        vector<int> q(n);
        for (int i=0;i<n;i++) {
            q[p[i]-1]=i+1;
        }
        int f=find(p),s=find(q);
        for (int i=0;i<start;i++) {
            if (i%2==0) {
                cout<<s<<" ";
            }else {
                cout<<f<<" ";
            }
        }
        if (start%2==1) {
            p=q;
        }
    }
    if (start==n)return;
    vector<pair<int,int>> cnt;
    int f=0,s=0;
    if (a[start]==1) {
        f++;
    }else {
        s++;
    }
    cnt.emplace_back(f,s);
    vector<int> b={a[start]};
    for (int i=start+1;i<m;i++) {
        if (a[i]==3) {
            if (a[i-1]==2) {
                s--;
                f++;
            }else {
                s++;
                f--;
            }
        }else if (a[i]==2) {
            s++;
        }else {
            f++;
        }
        cnt.emplace_back(f%2,s%2);
    }
    vector<vector<int>> c(2,vector<int>(2,0));
    c[0][0]=find(p);
    reverse(p.begin(),p.end());
    int c10=find(p);
    c[1][0]=c10;
    reverse(p.begin(),p.end());
    for (int i=0;i<n;i++) {
        p[i]=n-p[i]+1;
    }

    c[0][1]=find(p);
    reverse(p.begin(),p.end());
    c[1][1]=find(p);
    for (auto [l,r]:cnt) {
        cout<<c[l][r]<<" ";
    }cout<<endl;
}