#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define f first
#define s second
#define ll long long
#define ld long double

using namespace std;

int mod = 998244353;

int binpow(int a, int n) {
    if (n == 0) return 1;
    if (n % 2 == 1) return binpow(a, n - 1) * a % mod;
    int ans = binpow(a, n / 2);
    return ans * ans % mod;
}

int get(vector<int> a, int n) {
    int sum = 0;
    for (int i = 0; i < n; i++) {
        sum += binpow(a[i], i + 1);
        sum %= mod;
    }
    return sum;
}

void solve() {
    int n, m; cin >> n >> m;
    vector<int> p(n); for (int i = 0; i < n; i++) cin >> p[i];
    int cntp = 0, cntr = 0;
    vector<int> ras = p, per = p, tog = p;
    int st = get(p, n);
    reverse(ras.begin(), ras.end());
    for (int i = 0; i < n; i++) {
        per[i] = n + 1 - per[i];
    }
    tog = per;
    reverse(tog.begin(), tog.end());
    int r = get(ras, n), tv = get(tog, n), pv = get(per, n);
    vector<int> q(n);
    for (int i = 0; i < n; i++) {
        q[p[i] - 1] = i + 1;
    }
    int obr = get(q, n);
    int cnt = 0;
    for (int o = 0; o < m; o++) {
        int t; cin >> t;
        if (cnt == 0) {
            cout << st << " ";
        } else if (t == 1) cout << r << " ";
        else if (t == 2) cout << pv << " ";
        else cout << obr << " ";
        cnt++;
        cnt %= 2;
    }
}

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