#include <bits/stdc++.h>

using namespace std;
using ll = long long;

vector<ll> b1(vector<ll> p, ll n)  {
    vector<ll> q(n);
    for (ll i = 0; i < n; ++i) {
        q[i] = p[n - i - 1];
    }
    return q;
}

vector<ll> b2(vector<ll> p, ll n) {
    vector<ll> q(n);
    for (ll i = 0; i < n; ++i) {
        q[i] = n - p[i] + 1;
    }
    return q;
}

vector<ll> b3(vector<ll> p, ll n) {
    vector<ll> q(n);
    for (ll i = 0; i < n; ++i) {
        q[p[i] - 1] = i + 1;
    }
    return q;
}

ll mod = 998244353;

ll my_pow(ll a, ll b) {
    ll C = __lg(b) + 2;
    vector<ll> lg(C);
    lg[0] = 1;
    lg[1] = a;
    for (ll i = 2; i < C; ++i) {
        lg[i] = lg[i - 1] * lg[i - 1] % mod;
    }

    ll ans = 1;
    for (ll i = 0; i < C - 1; ++i) {
        if ((b & (1 << i)) == (1 << i))
            ans *= lg[i + 1];
    }
    return ans;
}

ll get_sum(vector<ll> &p, ll n) {
    ll ans = 0;
    for (ll i = 0; i < n; ++i) {
        ans += my_pow(p[i], i + 1);
    }
    return ans;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    ll n, q;
    cin >> n >> q;

    vector<ll> p(n);
    for (ll i = 0; i < n; ++i)
        cin >> p[i];

    while (q--) {
        ll b;
        cin >> b;

        if (b == 1) {
            p = b1(p, n);
        }
        else if (b == 2) {
            p = b2(p, n);
        }
        else {
            p = b3(p, n);
        }

        cout << get_sum(p, n) << " ";
    }
}