#include <bits/stdc++.h>

using namespace std;

using ld = long double;
using ll = long long;

mt19937 gen(random_device{}());


uniform_real_distribution<ld> dis(0.0, 1e2);

ll binpow(ll x, ll p) {
    if (p == 1) {
        return x;
    }
    if (p % 2 == 1) {
        return binpow(x, p - 1) * x % 998244353;
    }
    ll g = binpow(x, p / 2);
    return g * g % 998244353;
}


void solve() {
    ll n, q;
    cin >> n >> q;
    vector <ll> p(n);
    for (int i = 0; i < n; i++) {
        cin >> p[i];
    }
    ll ans31 = 0;
    for (int j = 0; j < n; j++) {
        //cout << binpow(p[j], j + 1) << endl;
        ans31 = (ans31 + binpow(p[j], j + 1)) % 998244353;
    }
    vector <ll> t(n);
    for (int j = 0; j < n; j++) {
        t[p[j] - 1] = j + 1;
    }
    p = t;
    ll ans32 = 0;
    for (int j = 0; j < n; j++) {
        //cout << binpow(p[j], j + 1) << endl;
        ans32 = (ans32 + binpow(p[j], j + 1)) % 998244353;
    }
    vector <ll> d(n);
    for (int j = 0; j < n; j++) {
        d[p[j] - 1] = j + 1;
    }
    p = d;

    ll ans21 = 0;
    for (int j = 0; j < n; j++) {
        //cout << binpow(p[j], j + 1) << endl;
        ans21 = (ans21 + binpow(p[j], j + 1)) % 998244353;
    }
    for (int j = 0; j < n; j++) {
        p[j] = n - p[j] + 1;
    }
    ll ans22 = 0;
    for (int j = 0; j < n; j++) {
        ans22 = (ans22 + binpow(p[j], j + 1))% 998244353;
    }
    for (int j = 0; j < n; j++) {
        p[j] = n - p[j] + 1;
    }
    ll ans12 = 0;
    for (int j = 0; j < n; j++) {
        ans12 = (ans12 + binpow(p[j], j + 1))% 998244353;
    }
    for (int j = 0; j < n / 2; j++) {
        swap(p[j], p[n - j - 1]);
    }
    ll ans11 = 0;
    for (int j = 0; j < n; j++) {
        ans11 = (ans11 + binpow(p[j], j + 1))% 998244353;
    }
    for (int j = 0; j < n / 2; j++) {
        swap(p[j], p[n - j - 1]);
    }
    int u = 0;
    for (int i = 0; i < q; i++) {
        int x;
        cin >> x;
        if (x == 1) {
            if (u % 2 == 0) {
                cout << ans22 << " ";
            }
            else {
                cout << ans21 << " ";
            }
            u++;
        }
        else if (x == 2) {
            if (u % 2 == 0) {
                cout << ans22 << " ";
            }
            else {
                cout << ans21 << " ";
            }
            u++;
        }
        else {
            u--;
            if (u % 2 == 0) {
                cout << ans22 << " ";
            }
            else {
                cout << ans21 << " ";
            }
            u++;
        }
    }
}


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