#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
using vll = vector<ll>;

ll ans[2][2][2];
constexpr ll mod = 998244353;

ll bp(ll a, ll b) {
    a %= mod;
    if (b == 0) return 1;
    if (b == 1) return a;
    if (b & 1)
        return a * bp(a, b - 1) % mod;
    ll p = bp(a, b / 2);
    return p * p % mod;
}

ll cost(vector<ll> a) {
    ll res = 0;
    for (ll i = 0; i < a.size(); i++)
        (res += bp(a[i], i + 1)) %= mod;
    return res;
}

vector<ll> obr(vector<ll> a) {
    vector<ll> r(a.size());
    for (ll i = 0; i < a.size(); i++)
        r[a[i] - 1] = i + 1;
    return r;
}

vector<ll> rev(vector<ll> a) {
    vector<ll> b(a.begin(), a.end());
    reverse(b.begin(), b.end());
    return b;
}

vector<ll> typ2(vector<ll> b) {
    vector<ll> c(b.begin(), b.end());
    for (ll i = 0; i < b.size(); i++)
        c[i] = b.size() - c[i] + 1;
    return c;
}

void print(vector<ll> a) {
    for (ll &i: a)
        cout << i << ' ';
    cout << " - " << cost(a);
    cout << '\n';
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    ll n, q;
    cin >> n >> q;
    vector<ll> a(n);
    for (ll &i: a)
        cin >> i;

    if (n <= 5000 && q <= 5000) {
        for (ll i = 0; i < q; i++) {
            ll x;
            cin >> x;

            if (x == 1) a = rev(a);
            if (x == 2) a = typ2(a);
            if (x == 3) a = obr(a);

            cout << cost(a) << '\n';
        }
        return 0;
    }


    for (ll f1 = 0; f1 <= 1; f1++) {
        vector<ll> b(a.begin(), a.end());
        if (f1) b = rev(b);

        for (ll f2 = 0; f2 <= 1; f2++) {
            vector<ll> c(b.begin(), b.end());
            if (f2) c = typ2(c);

            for (ll f3 = 0; f3 <= 1; f3++) {
                vector<ll> d(c.begin(), c.end());
                if (f3) d = obr(d);
                ans[f1][f2][f3] = cost(d);
            }
        }
    }

    ll f1 = 0, f2 = 0, f3 = 0;
    for (ll i = 0; i < q; i++) {
        ll x;
        cin >> x;

        if (x == 1) f1 ^= 1;
        if (x == 2) f2 ^= 1;
        if (x == 3) f3 ^= 1;

        if (!f3)
            cout << ans[f1][f2][f3] << '\n';
        if (f3)
            cout << ans[f2 ^ 1][f1][f3] << '\n';
    }
}