
#include<bits/stdc++.h>

using namespace std;

using ll = long long;

#define f first
#define s second
#define pii pair<ll, ll>

ll const MOD = 998244353;

ll sum(ll a, ll b) {
    return (a + b) % MOD;
}

ll mult(ll a, ll b) {
    return a * b % MOD;
}

ll bin_pow(ll a, ll p) {
    if (p == 0) return 1;
    if (p == 1) return a;
    ll k = bin_pow(a, p / 2);
    return mult(mult(k, k), bin_pow(a, p % 2));
}

ll calc(vector<ll> &p) {
    ll res = 0;
    for (ll i = 0; i < p.size(); ++i) {
        //cout << i << " " << p[i] << ' ' << bin_pow(p[i], i + 1) << endl;
        res = sum(res, bin_pow(p[i], i + 1));
    }
    return res;
}


int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll n, q;
    cin >> n >> q;
    vector<ll> p(n), b(q);
    for (ll i = 0; i < n; ++i) {
        cin >> p[i];
    }
    for (ll i = 0; i < q; ++i) {
        cin >> b[i];
    }
    vector<ll> ans;
    ll c = 0;
    ll a0 = calc(p);
    reverse(p.begin(), p.end());
    ll a1 = calc(p);
    reverse(p.begin(), p.end());
    for (ll i = 0; i < q; ++i) {
        if (i > 1) {
            if (b[i] == 1) {
                if (c % 2 == 0) {
                    cout << a1 << " ";
                } else {
                    cout << a0 << ' ';
                }
                ++c;
            } else if (b[i] == 2) {
                if (c % 2 == 0) {
                    cout << a1 << " ";
                } else {
                    cout << a0 << ' ';
                }
                ++c;
            } else {
                if (c % 2 == 0) {
                    cout << a0 << " ";
                } else {
                    cout << a1 << " ";
                }
            }
            continue;
        }
        if (b[i] == 1) {
            reverse(p.begin(), p.end());
        } else if (b[i] == 2) {
            for (ll j = 0; j < n; ++j) {
                p[j] = n - p[j] + 1;
            }
        } else {
            vector<ll> buf = p;
            for (ll j = 0; j < n; ++j) {
                p[buf[j] - 1] = j + 1;
            }
        }
        ans.push_back(calc(p));
        cout << ans.back() << " ";
        //cout << calc(p) << " ";
    }

}

