#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;
using sll = __int128;

const int maxn = 1e5 + 10, mod = 998244353;

int p[maxn];
int nw[maxn];

int bpow(int x, int y) {
    if (y == 0) return 1;
    if (y & 1) return (ll)x * bpow(x, y - 1) % mod;
    int pw = bpow(x, y >> 1);
    return (ll)pw * pw % mod;
}

int get(int n) {
    int sm = 0;
    for (int i = 1; i <= n; i++) {
        sm += bpow(p[i], i);
        if (sm >= mod) sm -= mod;
    }
    return sm;
}

void solve() {
    int n, q;
    cin >> n >> q;
    for (int i = 1; i <= n ; i++) cin >> p[i];
    for (int _ = 0; _ < q; _++) {
        int t;
        cin >> t;
        if (t == 1) {
            for (int i = 1; i <= n; i++) {
                nw[i] = p[n - i + 1];
            }
        } else if (t == 2) {
            for (int i = 1; i <= n; i++) {
                nw[i] = n - p[i] + 1;
            }
        } else {
            for (int i = 1; i <= n; i++) {
                nw[p[i]] = i;
            }
        }
        for (int i = 1; i <= n; i++) p[i] = nw[i];
        cout << get(n) << ' ';
    }
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int test = 1;
    // cin >> test;
    while (test--) {
        solve();
    }
}