#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

#pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2")

#define int long long

#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define fi first
#define se second
#define rep(i,j,k) for (int (i) = (j); (i) < (k); ++i)
#define rrep(i,j,k) for (int (i) = (j); (i) >= (k); --i)
#define re return
#define pb push_back

using namespace std;
using namespace __gnu_pbds;

using ordered_set = tree<int, less<>, null_type, rb_tree_tag, tree_order_statistics_node_update>;
using ordered_multiset = tree<int, less_equal<>, null_type, rb_tree_tag, tree_order_statistics_node_update>;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using pii = pair<int, int>;
using vpii = vector<pii>;
using vvpii = vector<vpii>;
using si = set<int>;
using msi = multiset<int>;

void amin(int& a, int b) {
    a = min(a, b);
}

void amax(int& a, int b) {
    a = max(a, b);
}

void solve();

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    int tt = 1;
    // cin >> tt;
    while (tt--) {
        solve();
    }
}

/* ------------------------------------- ACTUAL CODE STARTS HERE ------------------------------------- */

int n, q;

vi f1(vi& a) {
    vi p = a;
    reverse(all(p));
    re p;
}

vi f2(vi& a) {
    vi p;
    for (int x : a) {
        p.pb(n - x + 1);
    }
    re p;
}

vi f3(vi& a) {
    vi p(n);
    rep(i, 0, n) {
        p[a[i] - 1] = i + 1;
    }
    re p;
}

int bin_pow(int a, int b, int mod) {
    if (b == 0) re 1;
    if (b % 2 == 0) {
        int x = bin_pow(a, b / 2, mod);
        re (x * x) % mod;
    }
    re (a * bin_pow(a, b - 1, mod)) % mod;
}

const int MOD = 998244353;

map<vi, int> ids;
map<pii, int> where;
int ns[10] {};

int cur_id = 1;

void f(vi& a) {
    ids[a] = cur_id++;
    int ans = 0;
    rep(i, 0, a.size()) {
        ans += bin_pow(a[i], i + 1, MOD);
        ans %= MOD;
    }
    ns[ids[a]] = ans;
    vi b = f1(a), c = f2(a), d = f3(a);
    if (ids.find(b) == ids.end()) {
        f(b);
    }
    if (ids.find(c) == ids.end()) {
        f(c);
    }
    if (ids.find(d) == ids.end()) {
        f(d);
    }
    where[{ids[a], 1}] = ids[b];
    where[{ids[a], 2}] = ids[c];
    where[{ids[a], 3}] = ids[d];
}

void solve() {
    cin >> n >> q;
    vi a(n);
    for (int& x : a) cin >> x;
    f(a);
    int cur_id = 1;
    rep(i, 0, q) {
        int type; cin >> type;
        if (type == 1) {
            cur_id = where[{cur_id, 1}];
        } else if (type == 2) {
            cur_id = where[{cur_id, 2}];
        } else {
            cur_id = where[{cur_id, 3}];
        }
        cout << ns[cur_id] << " ";
    }
    cout << "\n";
}