#include <bits/stdc++.h>
using namespace std;
using ll = long long;

void Solve();

int32_t main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll tt = 1;
    // cin >> tt;
    while (tt--) {
        Solve();
    }
    return 0;
}

# define ff first
# define ss second

const ll mod = 998244353;
ll mult(ll a, ll b) {
    return a * b % mod;
}

ll add(ll a, ll b) {
    return a + b % mod;
}

ll binpow(ll a, ll p) {
    ll ans = 1;
    while (p) {
        if (p & 1) ans = mult(ans, a);
        a = mult(a, a);
        p /= 2;
    }
    return ans;
}

void Solve() {
    ll n, q;
    cin >> n >> q;
    assert(n <= 10);
    vector<ll> a(n);
    for (auto &x : a) cin >> x;
    set<vector<ll>> st;
    map<vector<ll>, ll> ans;
    queue<vector<ll>> qq;
    qq.push(a);
    while (!qq.empty()) {
        vector<ll> v = qq.front();
        qq.pop();
        ll anss = 0;
        for (ll i = 0; i < n; ++i) {
            anss = add(anss, binpow(v[i], i + 1));
        }
        ans[v] = anss;
        reverse(v.begin(), v.end());
        if (!st.count(v)) {
            qq.push(v);
            st.insert(v);
        }
        reverse(v.begin(), v.end());
        for (ll i = 0; i < n; ++i) {
            v[i] = n - v[i] + 1;
        }
        if (!st.count(v)) {
            qq.push(v);
            st.insert(v);
        }
        for (ll i = 0; i < n; ++i) {
            v[i] = n - v[i] + 1;
        }
        vector<ll> v1(n);
        for (ll i = 0; i < n; ++i) {
            v1[v[i] - 1] = i + 1;
        }
        if (!st.count(v1)) {
            qq.push(v1);
            st.insert(v1);
        }
    }
    assert(st.size() <= 8);
    for (ll i = 0; i < q; ++i) {
        ll x;
        cin >> x;
        if (x == 1) {
            reverse(a.begin(), a.end());
        } else if (x == 2) {
            for (ll i = 0; i < n; ++i) {
                a[i] = n - a[i] + 1;
            }
        } else {
            vector<ll> v1(n);
            for (ll i = 0; i < n; ++i) {
                v1[a[i] - 1] = i + 1;
            }
            swap(a, v1);   
        }
        cout << ans[a] << '\n';
    }
}