#include <bits/stdc++.h>

using namespace std;

#define int long long
#define all(a) a.begin(), a.end()
#define revall(a) a.rbegin(), a.rend();

const int mod = 998244353;

int fastpow(int base, int power, int modx) {
    if (power == 0) {
        return 1;
    }
    if (power % 2 == 0) {
        int x = fastpow(base, power / 2, modx) % modx;
        return (x * x) % modx;
    } else {
        return (base * fastpow(base, power - 1, modx)) % modx;
    }
}

void solve() {
    int n, q; cin >> n >> q;
    vector<int> p(n + 1);
    for (int i = 1; i <= n; i++) {
        cin >> p[i];
    }
    while (q--) {
        int op; cin >> op;
        vector<int> vc(n + 1);
        if (op == 1) {
            for (int i = 1; i <= n; i++) {
                vc[i] = p[n - i + 1];
            }
        } else if (op == 2) {
            for (int i = 1; i <= n; i++) {
                vc[i] = n - p[i] + 1;
            }
        } else {
            for (int i = 1; i <= n; i++) {
                vc[p[i]] = i;
            }
        }
        p = vc;
        int ans = 0;
        for (int i = 1; i <= n; i++) {
            ans = (ans + fastpow(p[i], i, mod)) % mod;
        }
        cout << ans << " ";
    }
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int tt = 1;
    // cin >> tt;
    while (tt--) {
        solve();
    }
}