#define _GLIBCXX_DEBUG

#include <bits/stdc++.h>

using namespace std;
//#pragma GCC optimize("O3,Ofast,unroll-loops")
//#pragma GCC target("avx2,avx,bmi")
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// using ordered_set  = tree <int, null_type, less <int>, rb_tree_tag, tree_order_statistics_node_update>;

#define int long long

#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()

struct node {
    int val = INT_MAX, id;
};

struct segtree {
    vector<node> t;

    node merge(node a, node b) {
        node c;
        if (a.val < b.val) c = a;
        else c = b;
        return c;
    }

    void upd(int v, int tl, int tr, int pos, int x) {
        if (tl + 1 == tr) {
            t[v].val = x;
            t[v].id = tl;
            return;
        }
        if (pos < (tl + tr) / 2) {
            upd(v * 2, tl, (tl + tr) / 2, pos, x);
        } else {
            upd(v * 2 + 1, (tl + tr) / 2, tr, pos, x);
        }
        t[v] = merge(t[v * 2], t[v * 2 + 1]);
    }

    node get(int v, int tl, int tr, int ql, int qr) {
        if (qr <= tl || tr <= ql) return node();
        if (ql <= tl && tr <= qr) return t[v];
        return merge(get(v * 2, tl, (tl + tr) / 2, ql, qr), get(v * 2 + 1, (tl + tr) / 2, tr, ql, qr));
    }
};

int bpow(int a, int b, int mod) {
    if (!b) return 1;
    int x=bpow(a,b/2,mod);
    x=(x*x)%mod;
    if (b&1) x=(x*a)%mod;
    return x;
}

signed main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    //cout<<3;
#endif
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int m1 = 998244353;

    int n,q;
    cin>>n>>q;
    vector<int>p(n+1);
    for(int i=1;i<=n;++i) {
        cin>>p[i];
    }
    vector<int>ps(n+1);
    for(int i=1;i<=n;++i) {
        ps[p[i]]=i;
    }
    vector<int>ans(4,0);
    {
        int cu=0;

        for(int i=1;i<=n;++i) {
            cu+=bpow(p[i], i,m1);
            cu%=m1;
        }
        ans[0]=cu;
    }
    {
        int cu=0;

        for(int i=1;i<=n;++i) {
            cu+=bpow(p[i], n-i+1,m1);
            cu%=m1;
        }
        ans[1]=cu;
    }
    {
        int cu=0;

        for(int i=1;i<=n;++i) {
            cu+=bpow(p[i], ps[n-p[i]+1],m1);
            cu%=m1;
        }
        ans[2]=cu;
    }
    {
        int cu=0;

        for(int i=1;i<=n;++i) {
            cu+=bpow(p[i], n-ps[n-p[i]+1]+1,m1);
            cu%=m1;
        }
        ans[3]=cu;
    }
    int c = 0;
    while(q--) {
        int type;
        cin>>type;
        if (type == 1) {
            if (c == 0) c=1;
            else if (c==1) c=0;
            else if (c==2) c=3;
            else c=2;
        } else {
            if (c==0) c=2;
            else if (c==1) c=3;
            else if (c==2) c=0;
            else c=1;
        }
        //cout<<c<<' ';
        cout<<ans[c]<<' ';
    }
}



