#include <bits/stdc++.h>
using namespace std;
using ll = long long;

const ll mod = 998244353;

ll binpow(ll x, ll st) {
    if (st == 0) return 1;
    if (st == 1) return x % mod;
    ll bp = binpow(x, st / 2) % mod;
    if (st % 2 == 0) return bp % mod * bp % mod;
    return bp % mod * bp % mod * x % mod;
}

void solve() {
    ll n, q;
    cin >> n >> q;
    vector<ll> arr(n + 1);
    for (ll i = 1; i <= n; ++i) {
        cin >> arr[i];
    }
    vector<ll> p = arr;
    for (ll i = 0; i < q; ++i) {
        ll com;
        cin >> com;
        p = arr;
        if (com == 1) {
            for (ll i = 1; i <= n; ++i) {
                arr[i] = p[n - i + 1];
            }
        } else if (com == 2) {
            for (ll i = 1; i <= n; ++i) {
                arr[i] = n - p[i] + 1;
            }
        } else {
            for (ll i = 1; i <= n; ++i) {
                arr[p[i]] = i;
            }
        }
        ll ans = 0;
        for (ll i = 1; i <= n; ++i) {
            ans += binpow(arr[i], i);
            ans %= mod;
        }
        cout << ans << ' ';
    }
}

signed main() {
    ll t = 1;
    while (t--) {
        solve();
    }
}