#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define ld long double
#define pb push_back
#define f first
#define s second
#define all(x) x.begin(), x.end()
#define ub upper_bound
#define lb lower_bound
#define pll pair<ll, ll>

mt19937_64 rnd(chrono::steady_clock::now().time_since_epoch().count());

const ll mod = 998244353;

ll b_pow(ll n, ll st) {
    ll ans = 1;
    while (st > 0) {
        if (st % 2) {
            ans *= n;
            ans%=mod;
            st--;
        }
        else {
            st/=2;
            n*=n;
            n%=mod;
        }
    }
    return ans;
}

ll sum = 0;
ll q[100100];

ll n;

map<vector<ll>, ll> al;

vector<ll> ch1(vector<ll> p) {
    vector<ll> b = p;
    reverse(all(b));
    return b;
}

vector<ll> ch2(vector<ll> p) {
    vector<ll> b = p;
    for (int i = 0; i < n; i++) {
        b[i] = n - b[i] + 1;
    }
    return b;
}

vector<ll> ch3(vector<ll> p) {
    vector<ll> b(n);
    for (int i = 0; i < n; i++) {
        b[p[i] - 1] = i + 1;
    }
    return b;
}

vector<ll> p;

void solve() {
    ll m;
    cin >> n >> m;
    p.resize(n);
    vector<ll> op(3, 0);
    for (int i = 0; i < n; i++) {
        cin >> p[i];
    }
    if (n * m <= 1e7) {
        for (int i = 0; i < m; i++) {
            cin >> q[i];
            ll ans = 0;
            if (q[i] == 1) {
                for (int i = 0; i < n / 2; i++) {
                    swap(p[i], p[n - i - 1]);
                }
                for (int i = 0; i < n; i++) {
                    ans += b_pow(p[i], i + 1);
                    ans %= mod;
                }
            }
            if (q[i] == 2) {
                for (int i = 0; i < n; i++) {
                    p[i] = n - p[i] + 1;
                    ans += b_pow(p[i], i + 1);
                    ans %= mod;
                }
            }
            if (q[i] == 3) {
                vector<ll> b(n);
                for (int i = 0; i < n; i++) {
                    b[p[i] - 1] = i + 1;
                }
                swap(b, p);
                for (int i = 0; i < n; i++) {
                    ans += b_pow(p[i], i + 1);
                    ans %= mod;
                }
            }
          //  al.insert(p);
            while (al.size() > 8) {
                cout << -1 << endl;
            }
            cout << ans << ' ';
        }
    }
    else {
        for (int i = 0; i < m; i++) {
            cin >> q[i];
            ll ans = 0;
            op[q[i] - 1]++;
            op[q[i] - 1] %= 2;
            if (al.find(op) == al.end()) {
                vector<ll> b = p;
                ll ans = 0;
                if (op[0])
                    b = ch1(b);
                if (op[1])
                    b = ch2(b);
                if (op[2])
                    b = ch3(b);
                for (int i = 0; i < n; i++) {
                    ans += b_pow(b[i], i + 1);
                    ans %= mod;
                }
                al[op] = ans;
            }
            cout << al[op] << ' ';
        }
    }
    assert(al.size() <= 8);
}

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    ll t = 1;
    //cin >> t;
    while (t--) {
        solve();
        cout << '\n';
    }
    return 0;
}
