#include<iostream>
#pragma GCC optimize("Ofast")
using namespace std;

using ll = long long;
using ld = long double;

const ll infl = (ll)4e18 + 100;
const int infi = (int)1e9 + 100;
const int maxn = (int)1e6 + 10;
const int mod = 998244353;

int n, q;
int p[maxn][2];

inline int bpow(int a, int x) {
    if (x == 0)
        return 1;

    if (x % 2 == 0)
        return bpow((a * a) % mod, x / 2);
    return a * bpow(a, x - 1);
}

inline void solve() {
    cin >> n >> q;
    for (int i = 1; i <= n; ++i)
        cin >> p[i][0];

    int nw = 0;

    while (q--) {
        int tp; cin >> tp;
        if (tp == 1) {
            for (int i = 1; i <= n; ++i)
                p[n - i + 1][1 - nw] = p[i][nw];
        } else if (tp == 2) {
            for (int i = 1; i <= n; ++i)
                p[i][1 - nw] = n - p[i][nw] + 1;
        } else {
            for (int i = 1; i <= n; ++i)
                p[p[i][nw]][1 - nw] = i;
        }

        nw = 1 - nw;

        int ans = 0;
        for (int i = 1; i <= n; ++i)
            ans = (ans + bpow(p[i][nw], i)) % mod;

        cout << ans << " ";
    }
}

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

    int t_ = 1; // cin >> t_;
    while (t_--) {
        solve();
        cout << '\n';
    }

    return 0;
}