#pragma GCC optimize("Ofast, unroll-loops")

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using sll = __int128_t;

const int mod = 998244353;

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

void solve() {
    int n, q;
    cin >> n >> q;
    vector<int> a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    for (int i = 0; i < q; ++i) {
        int x;
        cin >> x;
        vector<int> b(n);
        if (x == 1) {
            for (int j = 0; j < n; ++j) {
                b[j] = a[n - j - 1];
            }
        }
        if (x == 2) {
            for (int j = 0; j < n; ++j) {
                b[j] = n - a[j] + 1;
            }
        }
        if (x == 3) {
            for (int j = 0; j < n; ++j) {
                b[a[j] - 1] = j + 1;
            }
        }
        a.swap(b);
        int ans = 0;
        for (int j = 0; j < n; ++j) {
            ans += fpow(a[j], j + 1);
            if (ans >= mod) {
                ans -= mod;
            }
        }
        cout << ans << ' ';
    }
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t = 1;
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    cin >> t;
#endif
    while (t--) {
        solve();
    }
    return 0;
}
