#include <bits/stdc++.h>
#define int long long

using namespace std;

const int mod = 998244353;

int fpow(int a, int pow) {
    if (pow == 0) return 1;
    if (pow % 2 == 1) return a * fpow(a, pow - 1) % mod;
    int hpow = fpow(a, pow / 2);
    return hpow * hpow % mod;
}

int calc(vector<int> p) {
    int n = p.size();
    int res = 0;
    for (int i = 0; i < n; i++) {
        res += fpow(p[i], i + 1);
        res %= mod;
    }
    return res;
}

int gethash(vector<int> p) {
    int n = p.size();
    int res = 0;
    for (int i = 0; i < n; i++) {
        res ^= fpow(p[i], i + 1 + 5);
        res %= mod;
    }
    return res;
}

signed main() {
    cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0);
    int n, q;
    cin >> n >> q;
    vector<int> a(n);
    for (int& i : a) cin >> i;
    unordered_map<int, vector<int>> to;
    unordered_map<int, vector<int>>  per;
    unordered_map<int, int> init;
    unordered_map<int, int> ans;
    int cur = gethash(a);
    to[cur] = {-1, -1, -1};
    per[cur] = a;
    init[cur] = 1;
    ans[cur] = calc(a);
    while (q--) {
        int t;
        cin >> t;
        t--;
        if (to[cur][t] != -1) {
            cur = to[cur][t];
        } else {
            a = per[cur];
            if (t == 0) {
                reverse(a.begin(), a.end());
            }
            if (t == 1) {
                for (int& i : a) i = n - i + 1;
            }
            if (t == 2) {
                vector<int> q(n);
                for (int i = 0; i < n; i++) {
                    q[a[i] - 1] = i + 1;
                }
                a = q;
            }
            int prv = cur;
            cur = gethash(a);
            to[prv][t] = cur;
            if (init[cur] == 0) {
                per[cur] = a;
                to[cur] = {-1, -1, -1};
                init[cur] = 1;
                ans[cur] = calc(a);
            }
        }
        cout << ans[cur] << " ";
    }
    cout << "\n";
}