#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>


using namespace std;
#define int long long
#define all(x) x.begin(), x.end()
using pii = pair<int, int>;
using ull = unsigned long long;



int gcd(int x, int y) {
    if (y == 0) {
        return x;
    }
    return gcd(y, x % y);
}

int binpow(int a, int n) {
    int res = 1;
    while (n > 0) {
        if (n & 1) {
            res *= a;
        }
        a *= a;
        n >>= 1;
    }
    return res;
}


signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin >> n;
    int k;
    cin >> k;
    vector<int> p(n + 1);
    for (int i = 1; i < n + 1; i++) {
        cin >> p[i];
    }
    vector<int> q(n + 1);
    while (k--) {
        int x; cin >> x;
        q.assign(n + 1, 0);
        if (x == 1) {
            for (int i = 1; i < n + 1; i++) {
                q[i] = p[n - i + 1];
            }
        } else if (x == 2) {
            for (int i = 1; i < n + 1; i++) {
                q[i] = n - p[i] + 1;
            }
        } else {
            for (int i = 1; i < n + 1; i++) {
                q[p[i]] = i;
            }
        }
        int res = 0;
        p = q;
        for (int i = 1; i < n + 1; i++) {
            res += binpow(p[i], i);
        }
        cout << res << " ";
    }
}