#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;
#define int ll

#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()

const int MOD = 988244353;

int binpow(int x, int st) {
    if (!st) return 1;
    if (st & 1) return (x * binpow(x, st - 1)) % MOD;
    return binpow((x * x) % MOD, st / 2);
}

void solve() {
    vector<vector<vector<int>>> ans(2, vector<vector<int>> (2, vector<int> (2)));
    int n, q;
    cin >> n >> q;
    vector<int> p(n);
    for (auto &i : p) cin >> i;
    for (auto &i : p) i--;
    for (int inv = 0; inv < 2; inv++) {
        for (int rev = 0; rev < 2; rev++) {
            for (int x = 0; x < 2; x++) {
                vector<int> cur = p;
                if (inv) {
                    for (int i = 0; i < n; i++) {
                        cur[p[i]] = i;
                    }
                }
                if (rev) {
                    reverse(all(cur));
                }
                if (x) {
                    for (int i = 0; i < n; i++) {
                        cur[i] = (n - cur[i] - 1);
                    }
                }
                for (int i = 0; i < n; i++) {
                    ans[inv][rev][x] = (ans[inv][rev][x] + binpow(cur[i] + 1, i + 1)) % MOD;
                }
            }
        }
    }
    int inv = 0;
    int rev = 0;
    int x = 0;
    while (q--) {
        int b;
        cin >> b;
        if (b == 1) {
            rev ^= 1;
        }
        if (b == 2) {
            x ^= 1;
        }
        if (b == 3) {
            inv ^= 1;
            swap(rev, x);
        }
        cout << ans[inv][rev][x] << " ";
    }
    cout << endl;
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int t = 1;
    // cin >> t;
    while (t--) {
        solve();
    }
}
