#include <bits/stdc++.h>

using namespace std;
using ll = long long;
const ll mod = 998'244'353;

#pragma GCC target("avx2")

ll binpow(ll x, ll n) {
    if (n == 0) return 1;
    if (n % 2 == 0) return binpow(x * x % mod, n / 2);
    return x * binpow(x, n - 1) % mod;
}

int ans[2][2][2];

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n, q; cin >> n >> q;
    vector<int> p(n);
    for (int& o : p) cin >> o;
    int rev = 0, flip = 0, inv = 0;
    while (q--) {
        ll b; cin >> b;
        if (b == 1) {
            rev ^= 1;
        } else if (b == 2) {
            flip ^= 1;
        } else {
            inv ^= 1;
        }
        if (ans[rev][flip][inv] == 0) {
            if (rev) reverse(p.begin(), p.end());
            if (flip) for (int i = 0; i < n; i++) p[i] = n - p[i] + 1;
            if (inv) {
                vector<int> q(n);
                for (int i = 0; i < n; i++) q[p[i] - 1] = i + 1;
                p = q;
            }
            ll res = 0;
            for (int i = 0; i < n; i++) {
                res = (res + binpow(p[i], i + 1)) % mod;
            }
            ans[rev][flip][inv] = res;
            if (rev) reverse(p.begin(), p.end());
            if (flip) for (int i = 0; i < n; i++) p[i] = n - p[i] + 1;
            if (inv) {
                vector<int> q(n);
                for (int i = 0; i < n; i++) q[p[i] - 1] = i + 1;
                p = q;
            }
        }
        cout << ans[rev][flip][inv] << ' ';
    }
    return 0;
}
