#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define int long long
#define all(arr) arr.begin(), arr.end()
#define rall(arr) arr.rbegin(), arr.rend()

typedef long long ll;
typedef long double ld;
typedef __int128 lll;

mt19937_64 rnd(0);
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

const int MOD = 998'244'353;

int bin_pow(int a, int n){
    if (n == 0){
        return 1;
    }
    if (n % 2 == 0){
        int p = bin_pow(a, n / 2);
        return (p * p) % MOD;
    }
    return (bin_pow(a, n - 1) * a) % MOD;
}

int f(int n, vector<int> p){
    int ans = 0;
    for (int i = 0; i < n; i++){
        ans += bin_pow(p[i], i + 1);
        ans %= MOD;
    }
    return ans;
}

vector<int> f1(int n, vector<int> p){
    reverse(all(p));
    return p;
}

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

vector<int> f3(int n, vector<int> p){
    vector<int> ans(n);
    for (int i = 0; i < n; i++){
        ans[p[i] - 1] = i + 1;
    }
    return ans;
}

void solve(){
    int n, q;
    cin >> n >> q;
    vector<int> p(n);
    for (int i = 0; i < n; i++){
        cin >> p[i];
    }
    int s_00, s_01, s_02, s_03, s_12, s_13, s_23, s_123;
    {
        s_00 = f(n, p);
        s_01 = f(n, f1(n, p));
        s_02 = f(n, f2(n, p));
        s_03 = f(n, f3(n, p));
        s_12 = f(n, f2(n, f1(n, p)));
        s_13 = f(n, f3(n, f1(n, p)));
        s_23 = f(n, f3(n, f2(n, p)));
        s_123 = f(n, f3(n, f2(n, f1(n, p))));
    }
    while (q--){
        int t;
        cin >> t;
        if (t == 1){
            int t_00 = s_01;
            int t_01 = s_00;
            int t_02 = s_12;
            int t_03 = s_13;
            int t_12 = s_02;
            int t_13 = s_03;
            int t_23 = s_123;
            int t_123 = s_23;
            s_00 = t_00;
            s_01 = t_01;
            s_02 = t_02;
            s_03 = t_03;
            s_12 = t_12;
            s_13 = t_13;
            s_23 = t_23;
            s_123 = t_123;
        } else if (t == 2){
            int t_00 = s_02;
            int t_01 = s_12;
            int t_02 = s_00;
            int t_03 = s_23;
            int t_12 = s_01;
            int t_13 = s_123;
            int t_23 = s_03;
            int t_123 = s_13;
            s_00 = t_00;
            s_01 = t_01;
            s_02 = t_02;
            s_03 = t_03;
            s_12 = t_12;
            s_13 = t_13;
            s_23 = t_23;
            s_123 = t_123;
        } else {
            int t_00 = s_03;
            int t_01 = s_13;
            int t_02 = s_23;
            int t_03 = s_00;
            int t_12 = s_123;
            int t_13 = s_01;
            int t_23 = s_02;
            int t_123 = s_12;
            s_00 = t_00;
            s_01 = t_01;
            s_02 = t_02;
            s_03 = t_03;
            s_12 = t_12;
            s_13 = t_13;
            s_23 = t_23;
            s_123 = t_123;
        }
        cout << s_00 << ' ';
    }
}

signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cout.setf(ios::fixed);
    cout.precision(10);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int t = 1;
    //cin >> t;
    while (t--){
        solve();
        cout << '\n';
    }
    return 0;
}
