#include <bits/stdc++.h>
using namespace std;
#define int int64_t
const int mod = 998244353;
int pr(int u, int n) {
    if (n == 0) {
        return 1;
    }
    if (n % 2 == 0) {
        int y = pr(u, n / 2);
        return (y * y) % mod;
    }
    else {
        return (pr(u, n - 1) * u) % mod;
    }
}
void solve() {
    int n, q;
    cin >> n >> q;
    vector<int> p(n + 1);
    for (int i = 1; i <= n; i++) {
        cin >> p[i];
    }
    vector<int> beg = p;
    int s1 = 0, s2 = 0, s3 = 0;
    for (int qq = 0; qq < q; qq++) {
        int t;
        cin >> t;
        vector<int> q(n + 1);
        if (t == 1) {
            s1 = (s1 + 1) % 2;
        }
        else if (t == 2) {
            s2 = (s2 + 1) % 2;
        }
        else {
            s3 = (s3 + 1) % 2;
        }
        p = beg;
        if (s3 == 1) {
            for (int i = 1; i <= n; i++) {
                q[p[i]] = i;
            }
            p = q;
        }
        if (s1 == 1) {
            for (int i = 1; i <= n; i++) {
                q[i] = p[n - i + 1];
            }
            p = q;
        }
        if (s2 == 1) {
            for (int i = 1; i <= n; i++) {
                q[i] = n - p[i] + 1;
            }
            p = q;
        }
        int sum = 0;
        for (int i = 1; i <= n; i++) {
            //cout << p[i] << ' ';
            sum = (sum + pr(p[i], i)) % mod;
        }
        cout << sum << ' ';
    }
}
int32_t main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    // cin >> t;
    while (t--) {
        solve();
    }
}