#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;
}

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;
    int idx = 0;
    int cur = calc(a);
    to[cur] = {-1, -1, -1};
    per[cur] = 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 = calc(a);
            to[prv][t] = cur;
            if (to.find(cur) == to.end()) {
                per[cur] = a;
                to[cur] = {-1, -1, -1};
            }
        }
        cout << cur << " ";
    }
    cout << "\n";
}