#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// #pragma GCC optimize("Ofast,unroll-loops")
// #pragma GCC target ("avx2,bmi")
using namespace std;
// using namespace __gnu_pbds;
#define int long long
// typedef tree<int,null_type,less<>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;
const int N = 1e6 + 10, mod = 998244353, mod1 = 1e9 + 7;
int ans[N], g[N][5];
vector<int> v[N];
map<pair<int,int>,int> mp;

int bp(int a,int b) {
    if (b == 0)return 1;
    if (b % 2 == 0) {
        int q = bp(a, b / 2);
        return (q * q) % mod;
    } else {
        int q = bp(a, b - 1);
        return (q * a) % mod;
    }
}

int bp1(int a,int b) {
    if (b == 0)return 1;
    if (b % 2 == 0) {
        int q = bp(a, b / 2);
        return (q * q) % mod1;
    } else {
        int q = bp(a, b - 1);
        return (q * a) % mod1;
    }
}

void solve() {
    int n, m;
    cin >> n >> m;
    vector<int> a(n);
    int e = 0;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        ans[1] += bp(a[i], i + 1);
        ans[1] %= mod;
        e += bp1(a[i], i + 1);
        e %= mod1;
    }
    v[1] = a;
    mp[{ans[1], e}] = 1;
    int q = 2;
    int cur = 1;
    while (m--) {
        int x;
        cin >> x;
        if (g[cur][x] == 0) {
            a = v[cur];
            if (x == 1) {
                reverse(a.begin(), a.end());
            } else if (x == 2) {
                for (int i = 0; i < n; i++)a[i] = n - a[i] + 1;
            } else {
                vector<int> p(n);
                for (int i = 0; i < n; i++)p[a[i] - 1] = i + 1;
                a = p;
            }
            int w = 0;
            e = 0;
            for (int i = 0; i < n; i++) {
                w += bp(a[i], i + 1);
                e += bp1(a[i], i + 1);
                e %= mod1;
                w %= mod;
            }
            if (mp[{w,e}] == 0) {
                ans[q] = w;
                mp[{ans[q],e}] = q;
                g[cur][x] = q;
                v[q] = a;
                cur = q;
                q++;
            } else {
                g[cur][x] = mp[{w,e}];
                cur = g[cur][x];
            }
        } else {
            cur = g[cur][x];
        }
        cout << ans[cur] << " ";
    }
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();
}
