#include <bits/stdc++.h>

using namespace std;

#define int long long
const int mod = 998244353;

int binpow(int a, int x) {
    int res = 1;
    while (x) {
        if (x & 1) {
            res *= a;
            res %= mod;
        }
        a *= a;
        a %= mod;
        x >>= 1;
    }
    return res;
}

void q2(vector<int>&p, int n) {
    for (auto &i: p) {
        i = n - i + 1;
    }
}

void q1(vector<int>&p, int n) {
    for (int i = 0; 2 * i < n; i++) {
        swap(p[i], p[n - i - 1]);
    }
}

void q3(vector<int>&p, int n) {
    vector<int>q(n);
    for (int i = 0; i < n; i++) {
        q[p[i] - 1] = i + 1;
    }
    p = q;
}

void solve() {
    int n, q;
    cin >> n >> q;
    vector<int>p(n);
    int mask = 0;
    map<int, int>ans;
    for (int i = 0; i < n; i++) {
        cin >> p[i];
        ans[0] += binpow(p[i], i + 1);
        ans[0] %= mod;
    }
    vector<vector<int>>vec(8, p);
    for (int i = 1; i < 8; i++) {
        if (i & 1) {
            q1(vec[i], n);
        }
        if ((i >> 1) & 1) {
            q2(vec[i], n);
        }
        if ((i >> 2) & 1) {
            q3(vec[i], n);
        }
        for (int j = 0; j < n; j++) {
            ans[i] += binpow(vec[i][j], j + 1);
            ans[i] %= mod;
        }
    }
    
    int t;
    int a = 0;
    for (int i = 0; i < q; i++) {
        cin >> t;
        a = 0;
        if (t == 1) {
            q1(p, n);
        }
        if (t == 2) {
            q2(p, n);
        }
        if (t == 3) {
            q3(p, n);
        }
        for (int j = 0; j < n; j++) {
            a += binpow(vec[i][j], j + 1);
            a %= mod;
        }
        cout << a << " ";
    }
}

signed main() {
    cin.tie(0);
    cout.tie(0);
    ios_base::sync_with_stdio(0);
    solve();
}