#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const ll inf = 2e18;
const ld eps = 1e-13;
using vl = vector<ll>;
using vvl = vector<vl>;
ll binpow(ll a, ll n) {
    if (n == 0) return 1;
    if (n == 1) return a;
    if (n % 2) return a * binpow(a, n - 1);
    return binpow(a * a, n / 2);
}
int main() {
//    ios::sync_with_stdio(0); cin.tie(0);
    ll n, q; cin >> n >> q;
    vector<ll> a(n);
    for (auto &i : a) cin >> i;
    while (q--) {
        ll tp; cin >> tp;
        vector<ll> a2(n, -1);
        for (ll i = 1; i <= n; i++) {
            if (tp == 1) {
                a2 = a;
                reverse(a2.begin(), a2.end());
            } else if (tp == 2) {
                a2[i-1] = n - a[i-1] + 1;
            } else {
//                cout << i << "\n";
                a2[a[i-1]-1] = i;
            }
        }
        a = a2;
//        for (auto &i : a) cout << i << " ";
//        cout << "\n";
        ll sum = 0;
        for (ll i = 0; i < n; i++) {
            sum += binpow(a[i], i + 1);
        }
        cout << sum << " ";
    }
}
