#ifndef local
#pragma GCC optimize("Ofast")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#endif

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
using ll = long long;
using ld = long double;
using str = string;
using hashmap = gp_hash_table<ll,ll>;
using hashset = gp_hash_table<ll, null_type>;
using ordset = tree<ll,null_type, less<>,rb_tree_tag, tree_order_statistics_node_update>;


auto SD = chrono::high_resolution_clock::now().time_since_epoch().count();
mt19937 rnd(SD);
const ll mod = 998244353;
ll add(ll a, ll b) {
    return((a%mod)+(b%mod))%mod;
}
ll mult(ll a, ll b) {
    return((a%mod)*(b%mod))%mod;
}
ll pow_(ll e, ll n) {
    ll i = 1;
    while (n) {
        if ((n&1)) {
            i = mult(i, e);
        }e=mult(e,e);
        n >>= 1;
    }return i;
}

ll cnt_sum(vector<ll>p) {
    ll ans = 0;
    for (int i= 0; i < p.size(); i++) {
        ans =add(ans, pow_(p[i],i+1));
    }return ans;
}
vector<ll> p(vector<ll>o) {
    vector<ll> ans(o.size());
    for (int i = 0; i < o.size(); i++) {
        ans[i] = o.size()+1-o[i];
    }return ans;
}vector<ll> inv(vector<ll>o) {
    vector<ll> ans(o.size());
    for (int i = 0; i < o.size(); i++) {
        ans[o[i]-1]=i+1;
    }return ans;
}
vector<ll> rev(vector<ll>o) {
    vector<ll> ans = o;reverse(ans.begin(), ans.end());
    return ans;
}
void sol() {
    ll n, q;
    cin>>n>>q;
    vector<ll>o1(n);
    for (int i = 0; i<n; i++) {
        cin>>o1[i];
    }
   // cout<<endl;

    vector<ll> val_(4);
    val_[0] = cnt_sum(o1);
    val_[1] = cnt_sum(p(o1));
    val_[2] = cnt_sum(rev(p(o1)));
    val_[3] = cnt_sum(p(rev(p(o1))));

    ll a1 = 1;
    ll p1 = 1;
    ll p2 = 1;
    ll pr=-1;
    for (int j = 0; j < q; j++) {
        ll val;
        cin>>val;
        if (val ==2) {
          //  o1=p(o1);
          //  rev_inv_o =
            vector<ll> nw(4);
            for (int i = 0; i < 4; i++) {
                nw[i ] =val_[(i+p1+4)%4];
            }
            swap(val_, nw);
             p1 *= -1;
        }
        else if (val==1) {
            //o1=rev(o1);
            vector<ll> nw(4);
            for (int i = 0; i <4; i++) {
                nw[i ] =val_[(i+p2+4)%4];
            }
            swap(val_, nw);
            p2 *= -1;
        }
        else {
            o1= inv(o1);
        }
       // cout<<cnt_sum(o1)<<" ";
        cout<<val_[0]<<" ";
    }

}
signed main() {
#ifdef local
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    cout<<fixed<<setprecision(19);
    ll t1 = 1;
    //cin>>t1
    while (t1--) {
        sol();
    }
}