#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <experimental/random>
using namespace std;
using namespace __gnu_pbds;
using ll = long long;
using ordered_set = tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>;
using str = string;
using ld = long double;
const ll INF = 1e18, MOD = 998244353;
#define int long long
int binp(int v,int x) {
    if (x == 0) {
        return 1;
    }
    auto w = binp(v, x / 2);
    if (x % 2) {
        return w * w % MOD * v % MOD;
    }
    return w * w % MOD;
}

void solve();

signed main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int _ = 1;
    //cin >> _;
    while (_--) {
        solve();
        //cout << "\n";
    }
}

int calc(vector<int> p) {
    int ans = 0;
    for (int i = 0; i < p.size(); i++) {
        ans += binp(p[i], i + 1);
        ans %= MOD;
    }
    return ans;
}

void solve() {
    int n, q;
    cin >> n >> q;
    vector<int> p(n), p1(n), p2(n), p3(n);
    for (int i = 0; i < n; ++i) {
        cin >> p[i];
    }
    for (int i = 0; i < n; ++i) {
        p1[i] = p[n - 1 - i];
        p3[i] = n + 1 - p[n - 1 - i];
        p2[i] = n + 1 - p[i];
    }
    int ans = calc(p), ans1 = calc(p1), ans2 = calc(p2), ans3 = calc(p3);

    while (q--) {
        int b;
        cin >> b;
        if (b == 1) {
            swap(p, p1);
            swap(ans, ans1);
            swap(p2, p3);
            swap(ans2, ans3);
        } else if (b == 2) {
            swap(p, p2);
            swap(ans, ans2);
            swap(p1, p3);
            swap(ans1, ans3);
        } else {
            vector<int> c(n);
            for (int i = 0; i < n; ++i) {
                c[p[i] - 1] = i + 1;
            }
            swap(c, p);
            for (int i = 0; i < n; ++i) {
                c[p1[i] - 1] = i + 1;
            }
            swap(c, p1);
            for (int i = 0; i < n; ++i) {
                c[p2[i] - 1] = i + 1;
            }
            swap(c, p2);
            for (int i = 0; i < n; ++i) {
                c[p3[i] - 1] = i + 1;
            }
            swap(c, p3);
        }
        cout << ans << "\n";
    }
}
