#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;
}

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;

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

        if (i > 1) {
            cout << (i % 2 ? ans1 : ans0) << '\n';
            continue;
        }

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

        if (i == 0)
            ans0 = cost(a);
        if (i == 1)
            ans1 = cost(a);

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