#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 solve() {
    int n, q;
    cin >> n >> q;
    vector<ll> p(n);
    vector<ll> b(q);
    bool rev = false;
    bool ups = false;
    for (int i = 0; i < n; i++) {
        cin >> p[i];
    }

    for (int s = 0; s < q; s++) {
        cin >> b[s];
    }
    ll ans1 = 0;
    ll ans2 = 0; // ru
    ll ans3 = 0; // r
    ll ans4 = 0; // u
    for (ll i = 0; i < n; i++) {
        ans1 += bpow(p[i], i + 1);
        ans1 %= MOD;
    }
    reverse(p.begin(), p.end());
    for (ll i = 0; i < n; i++) {
        ans3 += bpow(p[i], i + 1);
        ans3 %= MOD;
    }
    for (int i = 0; i < n; i++) {
        p[i] = n + 1 - p[i];
    }
    for (ll i = 0; i < n; i++) {
        ans2 += bpow(p[i], i + 1);
        ans2 %= MOD;
    }
    reverse(p.begin(), p.end());
    for (ll i = 0; i < n; i++) {
        ans4 += bpow(p[i], i + 1);
        ans4 %= MOD;
    }
    /*
    if (b[0] == 3) {
        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;
        }
    }
    */
    for (int i = 0; i < q; i++) {
        if (b[i] == 1) {
            rev = 1 - rev;
        }
        if (b[i] == 2) {
            ups = 1 - ups;
        }
        if (!rev && !ups) {
            cout << ans1 << ' ';
        }
        if (rev && !ups) {
            cout << ans3 << ' ';
        }
        if (!rev && ups) {
            cout << ans4 << ' ';
        }
        if (rev && ups) {
            cout << ans2 << ' ';
        }
    }
}

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();
    }
}