#include <bits/stdc++.h>
#define int long long
#define ll long long

using namespace std;

const int mod = 998244353;

int bin_pow(int a, int n) {
    int ans = 1;
    while (n > 0) {
        if (n % 2) ans = (ans * a) % mod;
        a = (a * a) % mod;
        n /= 2;
    }
    return ans;
}

int ans[2][4];

void solve() {
    int n, q;
    cin >> n >> q;
    vector <int> p(n + 1);
    vector <int> rp(n + 1);

    for (int i = 1; i <= n; i++) {
        cin >> p[i];
        rp[p[i]] = i;
    }

    if (n <= 1000 && q <= 1000) {
        while (q--) {
            int qw;
            cin >> qw;
            int Ans = 0;
            if (qw == 1) {
                for (int i = 1; i <= n; i++)
                    rp[i] = p[n - i + 1];
                for (int i = 1; i <= n; i++) {
                    p[i] = rp[i];
                    Ans = (Ans + bin_pow(p[i], i)) % mod;
                }
                cout << Ans << " ";
            }
            else if (qw == 2) {
                for (int i = 1; i <= n; i++)
                    rp[i] = n - p[i] + 1;
                for (int i = 1; i <= n; i++) {
                    p[i] = rp[i];
                    Ans = (Ans + bin_pow(p[i], i)) % mod;
                }
                cout << Ans << " ";
            }
            else {
                for (int i = 1; i <= n; i++)
                    rp[p[i]] = i;
                for (int i = 1; i <= n; i++) {
                    p[i] = rp[i];
                    Ans = (Ans + bin_pow(p[i], i)) % mod;
                }
                cout << Ans << " ";
            }
        }
        return;
    }

    for (int i = 1; i <= n; i++) {
        ans[0][0] = (ans[0][0] + bin_pow(p[i], i)) % mod;
        ans[0][1] = (ans[0][1] + bin_pow(n - p[i] + 1, i)) % mod;
        ans[0][2] = (ans[0][2] + bin_pow(p[n - i + 1], i)) % mod;
        ans[0][3] = (ans[0][3] + bin_pow(n - p[n - i + 1] + 1, i)) % mod;

        ans[1][0] = (ans[1][0] + bin_pow(rp[i], i)) % mod;
        ans[1][1] = (ans[1][1] + bin_pow(n - rp[i] + 1, i)) % mod;
        ans[1][2] = (ans[1][2] + bin_pow(rp[n - i + 1], i)) % mod;
        ans[1][3] = (ans[1][3] + bin_pow(n - rp[n - i + 1] + 1, i)) % mod;
    }

    vector <int> b(q);
    bool fl = 1;
    for (int i = 0; i < q; i++) {
        cin >> b[i];
        if (b[i] == 3) fl = 0;
    }

//    cerr << ans[1] << " " << ans[2] << endl;
//
    if (!fl) {

        int i = 0, j = 0;
    //
        for (int e = 0; e < q; e++) {
            int qw = b[e];
            if (qw == 3) i = 1 - i;
            else if (qw == 2) {
                if (j == 0) j = 2;
                else if (j == 1) j = 3;
                else if (j == 2) j = 0;
                else j = 1;
            }
            else {
                if (j == 0) j = 1;
                else if (j == 1) j = 0;
                else if (j == 2) j = 3;
                else j = 2;
            }
            cout << ans[i][j] << " ";
        }
        return;
    }

    int i = 0, j = 0;
    //
        for (int e = 0; e < q; e++) {
            int qw = b[e];
            if (qw == 3) i = 1 - i;
            else if (qw == 2) {
                if (j == 0) j = 1;
                else if (j == 1) j = 3;
                else if (j == 2) j = 0;
                else j = 2;
            }
            else {
                if (j == 0) j = 2;
                else if (j == 1) j = 0;
                else if (j == 2) j = 3;
                else j = 1;
            }
            cout << ans[i][j] << " ";
        }

    return;
}

int32_t main() {
    ios_base:: sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);

    int t = 1;
    while (t--) solve();

    return 0;
}
