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

using namespace std;

const int mod = 998244353;

int binUp(int a, int n) {
    if (n == 0) {
        return 1;
    }

    int k = binUp(a, n / 2);
    if (n % 2 == 0) {
        return (k * k) % mod;
    }

    return (((k * k) % mod) * a) % mod;
}

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

    int n, q;
    cin >> n >> q;

    vector <int> a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }

    vector <int> b(q);
    for (int i = 0; i < q; ++i) {
        cin >> b[i];
    }

    vector <vector<vector<int>>> ans(2, vector <vector<int>> (2, vector <int> (2)));

    for (int r1 = 0; r1 < 2; ++r1) {
        for (int r2 = 0; r2 < 2; ++r2) {
            for (int s = 0; s < 2; ++s) {
                vector <int> p = a;

                int cur = 0;
                if (r1) {
                    std::reverse(p.begin(), p.end());
                }
                if (r2) {
                    for (int i = 0; i < n; ++i) {
                        p[i] = n - p[i] + 1;
                    }
                }
                if (s) {
                    vector <int> t(n);
                    for (int i = 0; i < n; ++i) {
                        t[p[i] - 1] = i + 1;
                    }

                    p = t;
                }

                for (int i = 0; i < n; ++i) {
                    cur = (cur + binUp(p[i], i + 1)) % mod;
                }

                ans[r1][r2][s] = cur;
            }
        }
    }

    int rev1 = 0, rev2 = 0, sw = 0;

    for (auto i : b) {
        if (i == 1) {
            if (sw) {
                rev2 = (rev2 + 1) % 2;
            }
            else {
                rev1 = (rev1 + 1) % 2;
            }
        }
        else if (i == 2) {
            if (sw) {
                rev1 = (rev1 + 1) % 2;
            }
            else {
                rev2 = (rev2 + 1) % 2;
            }
        }
        else {
            sw = (sw + 1) % 2;
        }

        cout << ans[rev1][rev2][sw] << " ";
    }

    return 0;
}