#include <bits/stdc++.h>

#define all(a) a.begin(), a.end()
#define pb push_back
#define lb lower_bound

using ll = long long;
using namespace std;

const int mod = 998244353;
const int inf = 1e9;
const int k = 475;

ll binpow(ll a, ll x) {
    if (x == 0) {
        return 1;
    }

    ll g = binpow(a, x/2);
    g = (g * g) % mod;
    if (x % 2) {
        return (g * a) % mod;
    }
    return g;
}

void read() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
}

int main() {
    read();

    int n, q;
    cin >> n >> q;

    vector<int> p(n);
    for (int i = 0; i < n; i++) {
        cin >> p[i];
    }

    auto get = [&](vector<int> &x) {
        ll cnt = 0;
        for (int i = 0; i < n; i++) {
            cnt = (cnt + binpow(x[i], i + 1)) % mod;
        }
        return cnt;
    };

    vector<int> b(q);
    for (int i = 0; i < q; i++) {
        cin >> b[i];
    }

    vector<int> revS(n), norevS(n), revNoS(n);
    for (int i = 0; i < n; i++) {
        revS[i] = n - p[n - i - 1] + 1;
        norevS[i] = n - p[i] + 1;
        revNoS[i] = p[n - i - 1];
    }

    vector<array<ll, 4>> s(n);

    ll ans0, ans1, ans2, ans3;
    ans0 = get(p);
    ans1 = get(revS);
    ans2 = get(norevS);
    ans3 = get(revNoS);
    vector<int> F(n);

    bool REV = false, SWAP = false;
    for (int i = 0; i < q; i++) {
        if (b[i] == 1) {
            REV = !REV;
        } else if (b[i] == 2) {
            SWAP = !SWAP;
        } else {
            for (int i = 0; i < n; i++) {
                F[p[i] - 1] = i + 1;
            }
        }

        if (b[i] <= 2) {
            if (REV && SWAP) {
                cout << ans1 << " ";
            } else if (REV && !SWAP) {
                cout << ans3 << " ";
            } else if (!REV && SWAP) {
                cout << ans2 << " ";
            } else {
                cout << ans0 << " ";
            }
        } else {
            cout << get(F) << "\n";
        }
    }

    cout << "\n";
}
