#include <bits/stdc++.h>

using namespace std;
using ll = long long;
const ll MOD = 998244353;

ll binpow(ll a, ll b) {
    if (b == 0) {
        return 1;
    }
    if (b == 1) {
        return a;
    }
    if (b % 2 == 0) {
        ll res = binpow(a, b / 2ll);
        return res * res % MOD;
    } else {
        ll res = binpow(a, b - 1);
        return res * a % MOD;
    }
}

ll func(vector<int> &arr) {
    ll res = 0;
    int n = (int) arr.size();
    for (int i = 1; i <= n; i++) {
        res += binpow(arr[i - 1], i);
        res %= MOD;
    }
    return res;
}

vector<int> func1(vector<int> &arr) {
    int n = (int) arr.size();
    vector<int> res(n);
    for (int i = 0; i < n; i++) {
        res[i] = arr[n - i - 1];
    }
    return res;
}

vector<int> func2(vector<int> &arr) {
    int n = (int) arr.size();
    vector<int> res(n);
    for (int i = 0; i < n; i++) {
        res[i] = n - arr[i] + 1;
    }
    return res;
}

vector<int> func3(vector<int> &arr) {
    int n = (int) arr.size();
    vector<int> res(n);
    for (int i = 0; i < n; i++) {
        res[arr[i] - 1] = i + 1;
    }
    return res;
}


int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n, q;
    cin >> n >> q;
    vector<int> arr(n);
    for (int i = 0; i < n; i++) {
        cin >> arr[i];
    }
    while (q--) {
        int type;
        cin >> type;
        if (type == 1) {
            arr = func1(arr);
        } else if (type == 2) {
            arr = func2(arr);
        } else {
            arr = func3(arr);
        }
        cout << func(arr) << ' ';
    }
    return 0;
}