#include <bits/stdc++.h>
#define int long long

using namespace std;
const int MOD = 998'244'353;

ostream& operator<<(ostream& out, vector<int> v) {
    for (auto el : v) {
        out << el << " ";
    }
    return out << "\n";
}

vector<int> a, b;
map<vector<int>, int> mp;
vector<int> ans(20);
vector<map<int, int>> g(20);
vector<vector<int>> v(20);
int n;
int last = 2;

void solve1() {
    for (int i = 0; i < n; i++) {
        b[i] = a[n - i - 1];
    }
    swap(a, b);
}
void solve2() {
    for (int i = 0; i < n; i++) {
        b[i] = n - a[i] + 1;
    }
    swap(a, b);
}
void solve3() {
    for (int i = 0; i < n; i++) {
        b[a[i] - 1] = i + 1;
    }
    swap(a, b);
}
int bin_pow(int x, int m) {
    if (m == 0) return 1;
    int y = bin_pow(x, m / 2);
    y = y * y % MOD;
    if (m & 1) y = x * y % MOD;
    return y;
}
int get() {
    int sum = 0;
    for (int i = 0; i < n; i++) {
        sum += bin_pow(a[i], i + 1);
    }
    return sum % MOD;
}

void solve() {
    int q;
    cin >> n >> q;
    a.resize(n);
    b.resize(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    v[1] = a;
    mp[a] = 1;
    ans[1] = get();
    int i = 1;
    while (q--) {
        int t;
        cin >> t;
        if (g[i][t] == 0) {
            a = v[i];
            if (t == 1) solve1();
            if (t == 2) solve2();
            if (t == 3) solve3();
            if (mp[a] == 0) {
                mp[a] = last;
                ans[last] = get();
                v[last] = a;
                last++;
            }
            g[i][t] = mp[a];
        }
        i = g[i][t];
        cout << ans[i] << "\n";
    }
}

signed main() {
//    ios_base::sync_with_stdio(0);
//    cin.tie(0);

    int t = 1;
//    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}
/*
4 0
4 1 2 3

*/
