#include <bits/stdc++.h>
// #pragma GCC optimize("O3")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;
using ll = long long;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

void solve();

void solve1() {
    auto start = clock();
    while (true) {
        auto now = clock();
        if ((now - start) / CLOCKS_PER_SEC > 1) {
            cout << 2;
            return;
        }
    }
}

int32_t main() {
#ifdef LOCAL
    freopen("Input.txt", "r",stdin);
#endif
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    int t = 1;
    // cin >> t;
    while (t--) {
        solve();
    }
}

vector<int> razv(const vector<int> &p) {
    vector<int> ans(p.size());
    for (int i = 0; i < p.size(); ++i) {
        ans[p.size() - i - 1] = p[i];
    }
    return ans;
}

vector<int> perev(const vector<int> &p) {
    vector<int> ans(p.size());
    for (int i = 0; i < p.size(); ++i) {
        ans[i] = p.size() - p[i] + 1;
    }
    return ans;
}

vector<int> obr(const vector<int> &p) {
    vector<int> ans(p.size());
    for (int i = 0; i < p.size(); ++i) {
        ans[p[i] - 1] = i + 1;
    }
    return ans;
}

constexpr int MOD = 998244353;

ll pw(ll n, ll st) {
    if (!st) {
        return 1;
    }
    if (st & 1) {
        return pw(n, st - 1) * n % MOD;
    }
    ll d = pw(n, st >> 1);
    return d * d % MOD;
}

ll sum(const vector<int> &p) {
    ll ans = 0;
    for (int i = 0; i < p.size(); ++i) {
        ans += pw(p[i], i + 1);
        ans %= MOD;
    }
    return ans;
}

void solve() {
    int n, q;
    cin >> n >> q;
    vector<int> p(n);
    for (int i = 0; i < p.size(); ++i) {
        cin >> p[i];
    }
    map<vector<int>, int> mp;
    vector<vector<int> > mass = {p};
    gp_hash_table<int, ll> ans;
    int cnt = 0;

    ans[cnt] = sum(p);
    mp[p] = cnt++;

    vector<vector<int> > graph(1, {-1, -1, -1});
    int ind = 0;
    while (q--) {
        int t;
        cin >> t;
        if (t == 1) {
            if (graph[ind][t - 1] == -1) {
                p = razv(mass[ind]);
                if (mp.find(p) == mp.end()) {
                    ans[cnt] = sum(p);
                    mp[p] = cnt++;
                    mass.emplace_back(p);
                    graph.push_back({-1,-1,-1});
                }
                graph[ind][t - 1] = mp[p];
            }
            ind = graph[ind][t - 1];
            cout << ans[ind] << '\n';
            continue;
        }
        if (t == 2) {
            if (graph[ind][t - 1] == -1) {
                p = perev(mass[ind]);
                if (mp.find(p) == mp.end()) {
                    ans[cnt] = sum(p);
                    mp[p] = cnt++;
                    mass.emplace_back(p);
                    graph.push_back({-1,-1,-1});
                }
                graph[ind][t - 1] = mp[p];
            }
            ind = graph[ind][t - 1];
            cout << ans[ind] << '\n';
            continue;
        }
        if (t == 3) {
            if (graph[ind][t - 1] == -1) {
                p = obr(mass[ind]);
                if (mp.find(p) == mp.end()) {
                    ans[cnt] = sum(p);
                    mp[p] = cnt++;
                    mass.emplace_back(p);
                    graph.push_back({-1,-1,-1});
                }
                graph[ind][t - 1] = mp[p];
            }
            ind = graph[ind][t - 1];
            cout << ans[ind] << '\n';
            continue;
        }
    }
}
