#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
using namespace std;
const int MOD = 998244353;
int add(int a, int b) {
    if (a + b >= MOD) return a + b - MOD;
    return a + b;
}
int mul(int a, int b) {
    return (ll)a * b % MOD;
}
int binpow(int a, int n) {
    if (n == 0) return 1;
    if (n & 1) return mul(a, binpow(a, n - 1));
    return binpow(mul(a, a), n / 2);
}
signed main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n, q; cin >> n >> q;
    vector<int> p(n + 1);
    for (int i = 1; i <= n; i++) cin >> p[i];
    vector<int> pos(n + 1);
    vector<int> pos2(n + 1);
    for (int i = 1; i <= n; i++) {
        pos[p[i]] = i;
        pos2[p[n - i + 1]] = i;
    }
    int s1 = 0;
    int s2 = 0;
    int s3 = 0;
    int s4 = 0;
    int s5 = 0;
    int s6 = 0;
    int s7 = 0;
    int s8 = 0;
    for (int i = 1; i <= n; i++) {
        s1 = add(s1, binpow(p[i], i));
        s2 = add(s2, binpow(n - p[i] + 1, i));
        s3 = add(s3, binpow(p[n - i + 1], i));
        s4 = add(s4, binpow(n - p[n - i + 1] + 1, i));
        s5 = add(s5, binpow(pos[i], i));
        s6 = add(s6, binpow(n - pos[i] + 1, i));
        s7 = add(s7, binpow(pos[n - i + 1], i));
        s8 = add(s8, binpow(n - pos[n - i + 1] + 1, i));
    }
    while (q--) {
        int t; cin >> t;
        if (t == 1) {
            swap(s1, s3); swap(s2, s4);
            swap(s5, s7); swap(s6, s8);
        } else if (t == 2) {
            swap(s1, s2); swap(s3, s4);
            swap(s5, s6); swap(s7, s8);
        } else {
            swap(s1, s5); swap(s2, s6);
            swap(s3, s7); swap(s4, s8);
        }
         cout << s1 << ' ';

    }
    cout << '\n';
    return 0;
}