#include <bits/stdc++.h>

using namespace std;
using ll = long long;
const int mod = 998244353;

ll fast(ll x, ll q) {
    if (q == 0) {
        return 1;
    }
    if (q % 2 == 0) {
        return fast((x * x) % mod, q / 2);
    }
    return (x * fast(x, q - 1)) % mod;
}

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n, q;
    cin >> n >> q;
    vector<ll> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        a[i]--;
    }
    ll ans = 0;
    vector<int> b(q);
    for (int i = 0; i < q; i++) {
        cin >> b[i];
        ans = 0;
        if (b[i] == 1) {
            for (int j = 0; j < n / 2; j++) {
                swap(a[j], a[n - j - 1]);
            }
        } else if (b[i] == 2) {
            for (int j = 0; j < n; j++) {
                a[j] = n - a[j] - 1;
            }
        } else {
            vector<ll> a1(n);
            for (int j = 0; j < n; j++) {
                a1[a[j]] = j;
            }
            swap(a, a1);
        }
        for (int j = 0; j < n; j++) {
            ans = (fast(a[j] + 1, j + 1) + ans) % mod;
        }
        cout << ans << ' ';
    }
}
