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

using namespace std;
using namespace __gnu_pbds;
using ll = long long;

#define int long long
#define all(x) x.begin(), x.end()

const int inf = 1e18;
const int mod = 998244353;

template<typename T>
using ordered_set = tree<T, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update>;

struct segTree {
    vector<int> t;
    int n;

    segTree(vector<int>& a) {
        n = a.size();
        t.assign(2 * n, 0);
        for (int i = 0; i < n; i++) t[i + n] = a[i];
        for (int i = n - 1; i >= 0; i--) t[i] = max(t[i << 1], t[(i << 1) | 1]);
    }

    int get(int l, int r) {
        int ans = -inf;
        l += n, r += n;
        while (l < r) {
            if (l & 1) ans = max(ans, t[l++]);
            if (r & 1) ans = max(ans, t[--r]);
            l >>= 1, r >>= 1;
        }
        return ans;
    }

    void upd(int i, int x) {
        i += n, t[i] = x, i >>= 1;
        while (i) t[i] = max(t[i << 1], t[(i << 1) | 1]), i >>= 1;
    }
};

struct Fen {
    vector<int> t;
    int n;

    Fen(int x) {n = x;t.assign(n + 1, 0);}
    int sum(int r) {int ans = 0;for (; r > 0; r -= r & -r) ans += t[r];return ans;}
    int get(int l, int r) {return sum(r) - sum(l - 1);}
    void upd(int i, int x) {for (; i <= n; i += i & -i) t[i] += x;}
};

map<vector<int>, int> mp;
int bpow(int a, int n) {
    int res = 1;

    while (n) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }

    return res;
}

void solve() {
    int n, q;
    cin >> n >> q;

    vector<int> a(n);
    for (int& i : a) cin >> i;

    auto f = [&](vector<int>& p) {
        int ans = 0;
        for (int i = 1; i <= n; i++) {
            int x = p[i - 1];
            int y = bpow(x, i);
            ans += y;
            ans %= mod;
        }

        return ans;
    };

    set<vector<int>> st;
    while (q--) {
        int t;
        cin >> t;

        if (t == 1) {
            vector<int> q(n);
            for (int i = 0; i < n; i++) q[i] = a[n - i - 1];
            a = q;
        } else if (t == 2) {
            vector<int> q(n);
            for (int i = 0; i < n; i++) q[i] = n - a[i] + 1;
            a = q;
        } else {
            vector<int> q(n);
            for (int i = 0; i < n; i++) q[a[i] - 1] = i + 1;
            a = q;
        }

        st.insert(a);
        cout << f(a) << " ";
        int sz = st.size();
        assert(sz <= 8);
    }

    cout << "\n";
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    #ifdef LOCAL
    freopen("input.txt", "r", stdin);
    #endif

    int t = 1;
    // cin >> t;

    while (t--) solve();
}