#include <bits/stdc++.h>
using innt = int;
#define int long long
using namespace std;
using ll = long long;
const int MOD = 998244353;
int binpow(int a, int p) {
    if (p == 1) return a % MOD;
    if (p % 2 == 0) return binpow(a, p/2) * binpow(a, p/2) % MOD;
    return binpow(a, p - 1) * a % MOD;
}
const int T = 1e5 + 10;
int p[T];

int f(int  n) {
    int ans = 0;
    for (int i = 1; i <= n; ++i) {
        ans = (ans + binpow(p[i - 1], i)) % MOD;
    }
    return ans;
}
signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, q;
    cin >> n >> q;
    for (int i = 0; i < n; ++i) {
        cin >> p[i];
    }
    for (int z = 0; z < q; ++z) {
        int b;
        cin >> b;
        vector<int> nw(n, 0);
        if (b == 1) {
            for (int i = 0;i < n; ++i) {
                nw[i] = p[n - i - 1];
            }
        } else if (b == 2) {
            for (int i = 0; i < n; ++i) {
                nw[i] = n - p[i] + 1;
            }
        } else {
            for (int i = 0; i < n; ++i) {
                nw[p[i] - 1] = i + 1;
            }
        }
        for (int i = 0; i < n; ++i) {
            p[i] = nw[i];
        }
        int ans = f(n);
        cout << ans << " ";
    }
    return 0;
}