#include <bits/stdc++.h>
using namespace std;

#pragma GCC optimize("O3", "Ofast", "unroll-loops", "fast-math")
#pragma GCC target("avx", "avx2")

using ll = long long;
using lll = __int128;
using dd = long double;
using para = pair <ll, ll>;
using parad = pair <dd, dd>;
const ll mod = 998244353;
ll binp(ll a, ll n) {
    if (n == 1) return a;
    if (!(n & 1)) return binp(a * a % mod, n >> 1);
    return a * binp(a, n - 1) % mod;
}
ll f(const vector <ll>& p) {
    ll n = p.size() - 1;
    ll ans = 0;
    for (ll i = 1; i <= n; ++i) {
        ans += binp(p[i], i);
        if (ans >= mod) ans -= mod;
    }
    return ans;
}
void solvetask() {
    ll n, qu;
    cin >> n >> qu;
    vector <ll> p(n + 1);
    for (ll i = 1; i <= n; ++i) {
        cin >> p[i];
    }
    set <ll> sb;
    vector <ll> b(qu);
    for (ll i = 0; i < qu; ++i) {
        cin >> b[i];
        sb.insert(b[i]);
    }
    vector <vector <ll>> qt(4, vector <ll> (n + 1));
    for (ll i = 1; i <= n; ++i) {
        qt[1][i] = p[n - i + 1];
    }
    for (ll i = 1; i <= n; ++i) {
        qt[2][i] = n - p[i] + 1;
    }
    for (ll i = 1; i <= n; ++i) {
        qt[3][p[i]] = i;
    }
    ll qtf[4];
    for (ll i = 1; i <= 3; ++i) {
        qtf[i] = f(qt[i]);
    }
    ll pcost = f(p);
    if (sb.size() == 1) {
        ll cntc = 0;
        for (ll i = 0; i < qu; ++i) {
            if (cntc == 0) {
                cout << pcost << ' ';
                cntc = 1;
            } else {
                cout << qtf[b[i]] << ' ';
                cntc = 0;
            }
        }
        return;
    }
    vector <ll> q(n + 1);
    for (ll ij = 0; ij < qu; ++ij) {
        if (b[ij] == 1) {
            for (ll i = 1; i <= n; ++i) {
                q[i] = p[n - i + 1];
            }
            for (ll i = 1; i <= n; ++i) {
                p[i] = q[i];
            }
            ll ans = 0;
            for (ll i = 1; i <= n; ++i) {
                ans += binp(p[i], i);
                if (ans >= mod) ans -= mod;
            }
            cout << ans << " ";
        } else if (b[ij] == 2) {
            for (ll i = 1; i <= n; ++i) {
                q[i] = n - p[i] + 1;
            }
            for (ll i = 1; i <= n; ++i) {
                p[i] = q[i];
            }
            ll ans = 0;
            for (ll i = 1; i <= n; ++i) {
                ans += binp(p[i], i);
                if (ans >= mod) ans -= mod;
            }
            cout << ans << " ";
        } else {
            for (ll i = 1; i <= n; ++i) {
                q[p[i]] = i;
            }
            for (ll i = 1; i <= n; ++i) {
                p[i] = q[i];
            }
            ll ans = 0;
            for (ll i = 1; i <= n; ++i) {
                ans += binp(p[i], i);
                if (ans >= mod) ans -= mod;
            }
            cout << ans << " ";
        }
    }
}
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll tests = 1;
    //cin >> tests;
    while (tests--) {
        solvetask();
    }
    return 0;
}