#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using db = double;
using pll = pair<ll ,ll>;
const ll LINF = 1ll<<62;
const db PI = acos(-1.0);
const ll P = 2777;
const ll MOD2 = 1e9 + 1000 - 7;
const ll MOD = 998244353;

struct segtree {
    int n;
    vector<int> t;
    vector<int> d;

    void push(int ind, int l, int r) {
        if (d[ind] == 0)
            return;
        t[ind] += d[ind] * (r - l);
        if (r - l > 1) {
            d[ind * 2 + 1] += d[ind];
            d[ind * 2 + 2] += d[ind];
        }
        d[ind] = 0;
    }

    segtree(int n) : n(n) {
        t.resize(n * 4);
        d.resize(n * 4);
    }

    void update(int ind, int l, int r, int ql, int qr, int add) {
        push(ind, l, r);
        if (r <= ql || qr <= l)
            return;
        if (ql <= l && r <= qr) {
            d[ind] += add;
            push(ind, l, r);
        }
        int m =(r + l) / 2;
        update(ind * 2 + 1, l, m, ql, qr, add);
        update(ind * 2 + 2, m, r, ql, qr, add);
        t[ind] = t[ind * 2 + 1] + t[ind * 2 + 2];
    }

    void update(int ql, int qr, int add) {
        update(0, 0, n, ql, qr, add);
    }

    ll query(int ind, int l, int r, int ql, int qr) {
        push(ind, l, r);
        if (r <= ql || qr <= l)
            return 0;
        if (ql <= l && r <= qr) {
            return t[ind];
        }
        int m =(r + l) / 2;
        return query(ind * 2 + 1, l, m, ql, qr) + query(ind * 2 + 2, m, r, ql, qr);
    }

    ll query(int ql, int qr) {
        return query(0, 0, n, ql, qr);
    }
};

ll bpow(ll x, ll step) {
    if (step == 0)
        return 1;
    ll z = bpow(x, step / 2);
    if (step % 2)
        return z * z % MOD * x % MOD;
    return z * z % MOD;
}

void first(vector<ll> &p) {
    reverse(p.begin(), p.end());
}
int n, q;
void second(vector<ll> &p) {
    for (int i = 0; i < n; i++) {
        p[i] = n + 1 - p[i];
    }
}

void third(vector<ll> &p) {
    vector<ll> w(n);
    for (int i = 0; i < n; i++) {
        w[i] = p[i];
    }
    for (int i = 0; i < n; i++) {
        p[w[i] - 1] = i + 1;
    }
}

ll count(vector<ll> &p) {
    ll ans = 0;
    for (ll i = 0; i < n; i++) {
        ans += bpow(p[i], i + 1);
        ans %= MOD;
    }
    return ans;
}


void solve() {

    cin >> n >> q;
    vector<ll> p(n);
    vector<ll> b(q);
    bool rev = false;
    bool ups = false;
    bool inv = false;
    for (int i = 0; i < n; i++) {
        cin >> p[i];
    }

    for (int s = 0; s < q; s++) {
        cin >> b[s];
    }
    ll ans1 = count(p);
    first(p);
    ll ans2 = count(p); // 1
    second(p);
    ll ans4 = count(p); // 12
    first(p);
    ll ans3 = count(p); // 2
    second(p);
    third(p);
    ll ans5 = count(p); // 3
    first(p);
    ll ans6 = count(p); // 13
    second(p);
    ll ans8 = count(p); // 123
    first(p);
    ll ans7 = count(p); // 23


    for (int i = 0; i < q; i++) {
        if (b[i] == 1) {
            rev = 1 - rev;
        }
        if (b[i] == 2) {
            ups = 1 - ups;
        }
        if (b[i] == 3) {
            if (inv) {
                if (ups && !rev) {
                    ups = 0;
                    rev = 1;
                }
                else if (!ups && rev) {
                    ups = 1;
                    rev = 0;
                }
            }
            inv = 1 - inv;
        }
        if (!inv && !ups && !rev) {
            cout << ans1 << ' ';
        }
        else if (!inv && ups && !rev) {
            cout << ans3 << ' ';
        }
        else if (!inv && !ups && rev) {
            cout << ans2 << ' ';
        }
        else if (!inv && ups && rev) {
            cout << ans4 << ' ';
        }
        else if (inv && !ups && !rev) {
            cout << ans5 << ' ';
        }
        else if (inv && ups && !rev) {
            cout << ans7 << ' ';
        }
        else if (inv && !ups && rev) {
            cout << ans6 << ' ';
        }
        else if (inv && ups && rev) {
            cout << ans8 << ' ';
        }
    }
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(10);
#ifdef HOLA
    freopen("input.txt", "r", stdin);
#endif
    int _t = 1;
    //cin >> _t;
    while (_t--) {
        solve();
    }
}