//#define _GLIBCXX_DEBUG
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cassert>
#include <iomanip>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>

#define int long long

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

const int INTI = 2e9 + 5, MAXN = 1e5 + 1, MOD = 998244353;
const ll INF = 4e18 + 5;

int binpow(int a, int b) {
    int pr = 1;
    while (b) {
        if (b & 1)
            pr = (pr * a) % MOD;
        b >>= 1;
        a = (a * a);
    }
    return pr;
}

void solve() {
    int n, q;
    cin >> n >> q;
    vector<int> a(n);
    for (auto& i : a)
        cin >> i;
    while (q--) {
        int b;
        cin >> b;
        if (b == 1) {
            for (int i = 0; i < n / 2; ++i)
                swap(a[i], a[n - i - 1]);
        }
        if (b == 2) {
            for (int i = 0; i < n; ++i)
                a[i] = n - a[i] + 1;
        }
        if (b == 3) {
            vector<int> q(n);
            for (int i = 0; i < n; ++i)
                q[a[i] - 1] = i + 1;
            a = q;
        }
        int sum = 0;
        for (int i = 0; i < n; ++i)
            sum += binpow(a[i], i + 1);
        cout << sum << ' ';
    }
}

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t = 1;
    //cin >> t;
    while (t--)
        solve();
    return 0;
}
