#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")

using namespace std;
using ll = long long;

# define len(x) ((ll) x.size())
const ll inf = 1e18;

const ll mod = 998244353;

ll binpow(ll a, ll b) {
    ll res = 1;
    while (b) {
        if (b & 1) { res = res * a % mod; }
        b >>= 1;
        a = a * a % mod;
    }
    return res;
}

ll brute(vector<ll>& a) {
    ll cnt = 0;
    for (ll i = 0; i < len(a); ++i) {
        cnt = (cnt + binpow(a[i], i + 1)) % mod;
    }
    return cnt;
}

void solve() {
    ll n, q;
    cin >> n >> q;

    vector<ll> p(n);
    for (ll i = 0; i < n; ++i) {
        cin >> p[i];
    }

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

            if (type == 1) {
                reverse(p.begin(), p.end());
            } else if (type == 2) {
                for (ll j = 0; j < len(p); ++j) {
                    p[j] = n - p[j] + 1;
                }
            } else {
                vector<ll> tmp = p;
                for (ll j = 0; j < len(tmp); ++j) {
                    tmp[p[j] - 1] = j + 1;
                }
                p = tmp;
            }

            cout << brute(p) << " ";
        }
        return;
    }

    bool reversed = false;
    ll last_r = -1;
    bool swapped = false;
    ll last_s = -1;
    bool inverted = false;
    ll last_i = -1;

    map<tuple<bool, bool, bool>, ll> mem;

    for (ll i = 0; i < q; ++i) {
        ll type;
        cin >> type;

        if (type == 1) {
            reversed = !reversed;
            last_r = i;
        } else if (type == 2) {
            swapped = !swapped;
            last_s = i;
        } else {
            inverted = !inverted;
            last_i = i;
        }

        if (mem.count({reversed, swapped, inverted})) {
            cout << mem[{reversed, swapped, inverted}] << " ";
            continue;
        }
        // cout << "new: " << reversed << " " << swapped << " " << inverted << endl;
        vector<ll> oth = p;
        if (reversed) {
            reverse(oth.begin(), oth.end());
        }
        if (inverted) {
            vector<ll> tmp = oth;
            for (ll j = 0; j < len(tmp); ++j) {
                tmp[oth[j] - 1] = j + 1;
            }
            oth = tmp;
        }
        if (swapped) {
            for (ll j = 0; j < len(oth); ++j) {
                oth[j] = n - oth[j] + 1;
            }
        }

        mem[{reversed, swapped, inverted}] = brute(oth);
        cout << mem[{reversed, swapped, inverted}] << " ";
    }
}

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

    solve();

    return 0;
}