#include <bits/stdc++.h>

using namespace std;
using ll = long long;
const ll MOD = 998244353;

ll rec(ll k, ll p) {
    if (p == 0) {
        return 1;
    }
    if (p % 2 == 1) {
        return rec(k, p - 1) * k % MOD;
    } else {
        ll r = rec(k, p / 2);
        return r * r % MOD;
    }
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    ll n, q;
    cin >> n >> q;
    vector<ll> p(n + 1);
    for (ll i = 1; i <= n; ++i) {
        cin >> p[i];
    }
    while (q--) {
        ll b;
        cin >> b;
        vector<ll> q(n + 1);
        if (b == 1) {
            for (ll i = 1; i <= n; ++i) {
                q[i] = p[n - i + 1];
            }
        }
        if (b == 2) {
            for (ll i = 1; i <= n; ++i) {
                q[i] = n - p[i] + 1;
            }
        }
        if (b == 3) {
            for (ll i = 1; i <= n; ++i) {
                q[p[i]] = i;
            }
        }
        ll ans = 0;
        for (ll i = 1; i <= n; ++i) {
            ans += rec(q[i], i);
            ans %= MOD;
        }
        cout << ans << ' ';
        swap(p, q);
    }
}
