#include <iostream>
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;
using lll = __int128;
using pii = pair <int, int>;
template <typename T>
using vec = vector <T>;

#define all(x) x.begin(), x.end()

template <typename T>
istream& operator >>(istream& in, vec <T>& a) {
    for (auto& x : a) in >> x;
    return in;
}
template <typename T>
ostream& operator <<(ostream& in, vec <T>& a) {
    for (auto& x : a) in << x << " ";
    return in;
}
template <typename T, typename Y>
istream& operator >>(istream& in, pair <T, Y>& a) {
    in >> a.first >> a.second;
    return in;
}
template <typename T, typename Y>
ostream& operator <<(ostream& in, pair <T, Y>& a) {
    in << a.first << " " << a.second;
    return in;
}
void FAST() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
}

const int INF = 1e9 + 7;
const ll LLINF = 1e18 + 7;
const ll MOD = 998'244'353;

ll binpow(ll val, ll st) {
    if (st == 0) return 1;
    ll a = binpow(val, st / 2);
    a = (a * a) % MOD;
    if (st % 2) a = (a * val) % MOD;
    return a;
}

ll coun(vec <int>& a) {
    ll ans = 0;
    int n = a.size();
    for (int i = 0; i < n; i++) {
        ans = (ans + binpow(a[i], i + 1)) % MOD;
    }
    return ans;
}

int main() {
    FAST();

    int n, q;
    cin >> n >> q;
    vec <int> a(n), b(q);
    cin >> a >> b;

    for (int u = 0; u < q; u++) {
        vec <int> res(n);
        if (b[u] == 1) {
            for (int i = 0; i < n; i++) {
                res[i] = a[n - i - 1];
            }
        }
        else if (b[u] == 2) {
            for (int i = 0; i < n; i++) {
                res[i] = n - a[i] + 1;
            }
        }
        else if (b[u] == 3) {
            for (int i = 0; i < n; i++) {
                    res[a[i] - 1] = i + 1;
            }
        }
        cout << coun(res) << " ";
        a = res;
    }
}