#include <bits/stdc++.h>
#define int long long
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define ff first
#define ss second

using namespace std;

const int mod = 998244353;

int binpow(int x, int y) {
    if (y == 0) return 1;
    if (y % 2 == 0) return binpow((x * x) % mod, y / 2);
    else return (x * binpow(x, y - 1)) % mod;
}

int sum(int a, int b) {
    if (a + b >= mod) return a + b - mod;
    return a + b;
}

void solve() {
    int n, q;
    cin >> n >> q;
    bool f = true;
    vector <int> a(n + 1);
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
        if (a[i] != i) {
            f = false;
        }
    }
    vector <int> req(q);
    for (int i = 0; i < q; ++i) {
        cin >> req[i];
    }
    if (f) {
        int ans1 = 0, ans2 = 0;
        for (int i = 1; i <= n; ++i) {
            ans1 = sum(ans1, binpow(a[i], i));
            ans2 = sum(ans2, binpow(a[i], n - i + 1));
        }
        bool rev = false;
        for (int i = 0; i < q; ++i) {
            if (req[i] < 3) {
                rev ^= 1;
            }
            if (rev) cout << ans2 << ' ';
            else cout << ans1 << ' ';
        }
        return;
    }
    int ans = 0;
    for (int i = 1; i <= n; ++i) {
        ans = sum(ans, binpow(a[i], i));
    }
    vector <int> b(n + 1, 0);
    int prew;
    for (int i = 0; i < q; ++i) {
        if (i == 0 || req[i] != req[i - 1]) {
            prew = ans;
            a.swap(b);
            if (req[i] == 1) {
                for (int j = 1; j <= n; ++j) {
                    a[j] = b[n - j + 1];
                }
            } else if (req[i] == 2) {
                for (int j = 1; j <= n; ++j) {
                    a[j] = n - b[j] + 1;
                }
            } else {
                for (int j = 1; j <= n; ++j) {
                    a[b[j]] = j;
                }
            }
            ans = 0;
            for (int j = 1; j <= n; ++j) {
                ans = sum(ans, binpow(a[j], j));
            }
        } else {
            swap(ans, prew);
            a.swap(b);
        }
        cout << ans << ' ';
    }
}

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    int t = 1;
    while (t--) solve(), cout << '\n';
}