#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define int long long

using namespace std;
using namespace __gnu_pbds;

#pragma GCC optimize("Ofast,unroll-loops,O3")
#pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma,tune=native")

typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;

const int MOD = 998244353;

int binpow(int a, int n) {
    int res = 1;

    while (n) {
        if (n & 1) {
            res = res * a % MOD;
        }
        a = a * a % MOD;
        n >>= 1;
    }

    return res;
}

int f(vector<int>& a) {
    int n = a.size();

    int res = 0;

    for (int i = 0; i < n; ++i) {
        res = (res + binpow(a[i], i + 1)) % MOD;
    }

    return res;
}

void upd1(vector<int>& p) {
    reverse(p.begin(), p.end());
}

void upd2(vector<int>& p) {
    int n = p.size();
    vector<int> tmp(n);
    for (int i = 0; i < n; ++i) {
        tmp[i] = n - p[i] + 1;
    }
    p = tmp;
}

void upd3(vector<int>& p) {
    int n = p.size();
    vector<int> tmp(n);
    for (int i = 0; i < n; ++i) {
        tmp[p[i] - 1] = i + 1;
    }
    p = tmp;
}

void print(vector<int>& p) {
    for (int& i : p) {
        cout << i << " ";
    }
    cout << "\n";
}

int32_t main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

//    random_device rnd;
//    mt19937 gen(rnd());
//    uniform_int_distribution<> dis(0, n - 1);
//    uniform_real_distribution<> dis1(0, 1);

    int n, q;
    cin >> n >> q;
    vector<int> p(n);

    for (int& i : p) {
        cin >> i;
    }

    int ans[2][2];

    for (int i = 0; i < 2; ++i) {
        if (i == 1) {
            upd1(p);
        }
        for (int j = 0; j < 2; ++j) {
            if (j == 1) {
                upd2(p);
            }
            ans[i][j] = f(p);
        }
        upd2(p);
    }

    upd1(p);

    int a = 0, b = 0;

    while (q--) {
        int t;
        cin >> t;
        if (t == 1) {
            a = (a + 1) % 2;
        }
        else if (t == 2) {
            b = (b + 1) % 2;
        }
        else {
            for (int i = 0; i < a; ++i) {
                upd1(p);
            }
            for (int i = 0; i < b; ++i) {
                upd2(p);
            }
            a = b = 0;
            upd3(p);
            for (int i = 0; i < 2; ++i) {
                if (i == 1) {
                    upd1(p);
                }
                for (int j = 0; j < 2; ++j) {
                    if (j == 1) {
                        upd2(p);
                    }
                    ans[i][j] = f(p);
                }
                upd2(p);
            }

            upd1(p);
        }
        cout << ans[a][b] << " ";
    }
}
