#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <set>
#include <queue>

using namespace std;

const long long MOD = 998244353;

long long binpow(long long a, long long b) {
    long long x = 1, c = a;
    vector<pair<long long, long long>> v = {{a, 1}};
    while (x * 2 <= b) {
        c *= c;
        c %= MOD;
        x *= 2;
        v.push_back({c, x});
    }
    long long x1 = x, k = 0;
    while (x1 > 0) {
        x1 /= 2;
        k++;
    }
    for (int j = k - 2; j >= 0; j--) {
        if (x + v[j].second <= b) {
            c *= v[j].first;
            c %= MOD;
            x += v[j].second;
        }
    }
    return c;
}

long long n;
long long ans0(vector<long long> v) {
    long long ans = 0;
    for (int i = 0; i < n; i++) {
        long long c = binpow(v[i], i + 1);
        ans += c;
        ans %= MOD;
    }
    return ans;
}
long long ans1(vector<long long> v) {
    long long ans = 0;
    reverse(v.begin(), v.end());
    for (int i = 0; i < n; i++) {
        long long c = binpow(v[i], i + 1);
        ans += c;
        ans %= MOD;
    }
    return ans;
}
long long ans2(vector<long long> v) {
    long long ans = 0;
    for (int i = 0; i < n; i++) {
        v[i] = n - v[i] + 1;
        long long c = binpow(v[i], i + 1);
        ans += c;
        ans %= MOD;
    }
    return ans;
}
long long ans3(vector<long long> v) {
    long long ans = 0;
    vector<long long> a(n);
    for (int i = 0; i < n; i++) {
        a[v[i] - 1] = i + 1;
    }
    for (int i = 0; i < n; i++) {
        ans += binpow(a[i], i + 1);
        ans %= MOD;
    }
    return ans;
}
long long ans12(vector<long long> v) {
    long long ans = 0;
    reverse(v.begin(), v.end());
    for (int i = 0; i < n; i++) {
        v[i] = n - v[i] + 1;
        long long c = binpow(v[i], i + 1);
        ans += c;
        ans %= MOD;
    }
    return ans;
}
long long ans13(vector<long long> v) {
    long long ans = 0;
    reverse(v.begin(), v.end());
    vector<long long> a(n);
    for (int i = 0; i < n; i++) {
        a[v[i] - 1] = i + 1;
    }
    for (int i = 0; i < n; i++) {
        ans += binpow(a[i], i + 1);
        ans %= MOD;
    }
    return ans;
}
long long ans23(vector<long long> v) {
    long long ans = 0;
    for (int i = 0; i < n; i++) {
        v[i] = n - v[i] + 1;
    }
    vector<long long> a(n);
    for (int i = 0; i < n; i++) {
        a[v[i] - 1] = i + 1;
    }
    for (int i = 0; i < n; i++) {
        ans += binpow(a[i], i + 1);
        ans %= MOD;
    }
    return ans;
}
long long ans123(vector<long long> v) {
    reverse(v.begin(), v.end());
    long long ans = 0;
    for (int i = 0; i < n; i++) {
        v[i] = n - v[i] + 1;
    }
    vector<long long> a(n);
    for (int i = 0; i < n; i++) {
        a[v[i] - 1] = i + 1;
    }
    for (int i = 0; i < n; i++) {
        ans += binpow(a[i], i + 1);
        ans %= MOD;
    }
    return ans;
}

int main() {
    int q;
    cin >> n >> q;
    
    if (n <= 1000 && q <= 1000) {
        vector<vector<long long>> p(n + 1, vector<long long>(n + 1, 1LL));

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                p[i][j] = p[i][j - 1] * i;
                p[i][j] %= MOD;
            }
        }

        vector<long long> v(n), b(q);
        for (int i = 0; i < n; i++) {
            cin >> v[i];
        }
        for (int i = 0; i < q; i++) {
            long long ans = 0;
            cin >> b[i];
            if (b[i] == 1) {
                vector<long long> a(n);
                for (int j = 0; j < n; j++) {
                    a[j] = v[n - j - 1];
                    ans += p[a[j]][j + 1];
                    ans %= MOD;
                }
                v = a;
            } else if (b[i] == 2) {
                vector<long long> a(n);
                for (int j = 0; j < n; j++) {
                    a[j] = n - v[j] + 1;
                    ans += p[a[j]][j + 1];
                    ans %= MOD;
                }
                v = a;
            } else {
                vector<long long> a(n);
                for (int j = 0; j < n; j++) {
                    a[v[j] - 1] = j + 1;
                }
                for (int j = 0; j < n; j++) {
                    ans += p[a[j]][j + 1];
                    ans %= MOD;
                }
                v = a;
            }
            cout << ans % MOD << ' ';
        }
        return 0;
    }
    
    
    
    
    
    vector<long long> v(n), b(q);
    for (int i = 0; i < n; i++) {
        cin >> v[i];
    }
    
    
    
    
    long long a0 = ans0(v), a1 = ans1(v), a2 = ans2(v), a3 = ans3(v), a12 = ans12(v), a13 = ans13(v), a23 = ans23(v), a123 = ans123(v);

    int ok1 = 0, ok2 = 0, ok3 = 0;
    for (int i = 0; i < q; i++) {
        int t; cin >> t;
        if (t == 1) {
            ok1++;
        } else if (t == 2) {
            ok2++;
        } else {
            ok3++;
        }
        ok1 %= 2;
        ok2 %= 2;
        ok3 %= 2;
        if (ok1 == 0 && ok2 == 0 && ok3 == 0) {
            cout << a0;
        } else if (ok1 == 1 && ok2 == 0 && ok3 == 0) {
            cout << a1;
        } else if (ok1 == 0 && ok2 == 1 && ok3 == 0) {
            cout << a2;
        } else if (ok1 == 0 && ok2 == 0 && ok3 == 1) {
            cout << a3;
        } else if (ok1 == 1 && ok2 == 1 && ok3 == 0) {
            cout << a12;
        } else if (ok1 == 1 && ok2 == 0 && ok3 == 1) {
            cout << a13;
        } else if (ok1 == 0 && ok2 == 1 && ok3 == 1) {
            cout << a23;
        } else {
            cout << a123;
        }
        cout << ' ';
    }
}
