#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <array>
#include <unordered_set>
#include <unordered_map>

using namespace std;
using ll = long long;
using ld = long double;

#define int ll

const int MOD = 998244353, MOD2 = 1e9 + 7;

struct Node {
    vector<int> a;

    pair<int, int> next[3] = {{-1, -1}, {-1, -1}, {-1, -1}};
};

int fpow(int v, int st, int mod) {
    if (st == 0) return 1;
    if (st == 1) return v;

    int res = fpow(v, st / 2, mod);
    res *= res;
    res %= mod;

    if (st % 2 == 1) {
        res *= v;
        res %= mod;
    }
    return res;
}

void op1(vector<int> &a) {
    reverse(a.begin(), a.end());
}

void op2(vector<int> &a) {
    for (auto &el : a)
        el = a.size() - el - 1;
}

void op3(vector<int> &a) {
    vector<int> res(a.size());
    for (int i = 0; i < a.size(); ++i)
        res[a[i]] = i;
    a = res;
}

void call(vector<int> &a, int act) {
    if (act == 0)
        op1(a);
    else if (act == 1)
        op2(a);
    else if (act == 2)
        op3(a);
}

pair<int, int> score(vector<int> &a) {
    int res1 = 0, res2 = 0;
    for (int i = 0; i < a.size(); ++i) {
        res1 += fpow(a[i] + 1, i + 1, MOD);
        res1 %= MOD;
        res2 += fpow(a[i] + 1, i + 1, MOD2);
        res2 %= MOD2;
    }
    return {res1, res2};
}

signed main() {
    int n, q;
    cin >> n >> q;

    vector<int> p(n);
    for (int i = 0; i < n; ++i) {
        cin >> p[i];
        p[i]--;
    }

    pair<int, int> now = score(p);
    map<pair<int, int>, Node> graph;
    graph[now] = {p};

    for (int _ = 0; _ < q; ++_) {
        int act;
        cin >> act;
        act--;

        auto &el = graph[now];
        if (el.next[act].first == -1) {
            auto b = el.a;
            call(b, act);

            pair<int, int> sx = score(b);
            el.next[act] = sx;

            if (graph.count(sx) == 0)
                graph[sx] = {b};
            graph[sx].next[act] = now;
            
        }

        now = el.next[act];
        cout << now.first << " ";
    }
}