#include <bits/stdc++.h>
using ll = long long;
#define int ll

using namespace std;

const int mod = 998244353;
struct Mint {
    int val;
    Mint() {
        val = 0;
    }
    Mint(int x) {
        x %= mod;
        if (x < 0) x += mod;
        val = x;
    }
};
Mint operator+=(Mint& a, Mint b) {
    a.val += b.val;
    if (a.val >= mod) a.val -= mod;
    return a;
}
Mint operator-=(Mint& a, Mint b) {
    a.val -= b.val;
    if (a.val < 0) a.val += mod;
    return a;
}
Mint operator+(Mint a, Mint b) {
    a += b;
    return a;
}
Mint operator-(Mint a, Mint b) {
    a -= b;
    return a;
}
Mint operator*=(Mint& a, Mint b) {
    a.val = 1ll * a.val * b.val % mod;
    return a;
}
Mint operator*(Mint a, Mint b) {
    a *= b;
    return a;
}
Mint binpow(Mint x, int p) {
    if (p == 0) return 1;
    if (p == 1) return x;
    if (p % 2 == 0) {
        return binpow(x * x, p / 2);
    }
    return x * binpow(x, p - 1);
}
void solve() {
    int n, qq; cin >> n >> qq;
    vector<int> p(n);
    for (int i = 0; i < n; i++) cin >> p[i];
    auto calc = [&](vector<int>& p) -> Mint {
        Mint res = 0;
        for (int i = 0; i < n; i++) {
            res += binpow(p[i], i + 1);
        }
        return res;
    };
    while (qq --> 0) {
        int b; cin >> b;
        if (b == 1) {
            vector<int> q(n);
            for (int i = 0; i < n; i++) {
                q[i] = p[n - i - 1];
            }
            cout << calc(q).val << " ";
            p = q;
        } else if (b == 2) {
            vector<int> q(n);
            for (int i = 0; i < n; i++) q[i] = n - p[i] + 1;
            cout << calc(q).val << " ";
            p = q;
        } else {
            vector<int> q(n);
            for (int i = 0; i < n; i++) {
                q[p[i] - 1] = i + 1;
            }
            cout << calc(q).val << " ";
            p = q;
        }
    }
}

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int t = 1;
    // cin >> t;
    while (t --> 0) solve();
}