#include <bits/stdc++.h>
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define sz size
#define pb push_back
//#define int long long

using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

const int A = 1e5 + 5;
const int MOD = 998244353;

ll n, q, i, tp, cnt = 1, mx = 2;
mt19937 rnd(time(0));
vector<int> a, b;
map<int, array<int, 5>> mp;
map<int, int> mp1;
map<vector<int>, int> mp2;
map<int, vector<int>> k;

int binpow(int x, int k) {
    int ans = 1;
//    return 1;
    while (k) {
        if (k % 2) ans = (ans * x) % MOD;
        x = (x * x) % MOD;
        k /= 2;
    }
    return ans;
}

int f() {
    int i, ans = 0;
    for (i = 1; i <= n; i++) {
//        cout << i << " ";
        ans = (ans + binpow(a[i], i)) % MOD;
    }
    return ans;
}

void Try1() {
    reverse(a.begin() + 1, a.begin() + n + 1);
}

void Try2() {
    int i;
    for (i = 1; i <= n; i++) a[i] = n - a[i] + 1;
}

void Try3() {
    int i;
    for (i = 1; i <= n; i++) b[a[i]] = i;
    for (i = 1; i <= n; i++) a[i] = b[i];
}

int32_t main() {
    #ifdef LOCAL
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif // LOCAL

    cin >> n >> q;
    a.resize(n + 2);
    b.resize(n + 2);
    for (i = 1; i <= n; i++) {
        cin >> a[i];
    }
//    cout << a.size() << endl;
    k[1] = a;
    mp1[1] = f();
//    return 0;
    mp2[a] = 1;
    while (q--) {
        cin >> tp;
        if (tp == 1) Try1();
        if (tp == 2) Try2();
        if (tp == 3) Try3();
        cout << f() << " ";
        continue;
        if (mp[cnt][tp] == 0) {
            a = k[cnt];
            if (mp2.count(a)) {
                mp[cnt][tp] = mp2[a];
                cnt = mp[cnt][tp];
            } else {
                mp[cnt][tp] = mx;
                k[mx] = a;
                mp1[mx] = f();
                mp2[a] = mx;
                cnt = mx;
                mx++;
            }
//            cout << mp1[cnt] << " ";
        } else {
            cnt = mp[cnt][tp];
            cout << mp1[cnt] << " ";
        }
    }
    return 0;
}
