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

using namespace std;

using ll = long long;

// template <class T> using ordered_set = tree<T, nullptr, less<T>, rb_tree_tag, tree_node_statistics_update>;

const ll inf = ll(1e18) + 2;
const ll mod = 998244353;

struct node {
    ll x;
    ll p;

    node(ll x_ = -inf, ll p_ = 0) {
        x = x_;
        p = p_;
    }
};

node c(node a, node b) {
    node j;
    j.x = a.x + b.x;
    j.p = 0;
    return j;
}

struct st {

};

ll bp(ll a, ll n) {
    if (n == 0) {
        return 1 % mod;
    }
    ll k = bp(a, n / 2);
    if (n % 2 == 0) {
        return (k * k) % mod;
    }
    return (((k * k) % mod) * a) % mod;
}

ll f(vector<ll> &a) {
    ll res = 0;
    for (ll i = 0; i < ll(a.size()); i++) {
        res += bp(a[i] + 1, i + 1);
        res %= mod;
    }
    return res;
}

map<vector<ll>, ll> ans;

void dfs(vector<ll> &a, map<vector<ll>, ll> &u, vector<ll> t, vector<ll>&a2) {
    u[a] = 1;
    ll n = (ll) a2.size();
    ans[a] = f(a2);

    t.push_back(0);
    reverse(a.begin(), a.end());
    reverse(a2.begin(), a2.end());
    if (u[a] == 0) {
        dfs(a, u, t, a2);
    }
    reverse(a.begin(), a.end());
    reverse(a2.begin(), a2.end());
    t.pop_back();

    t.push_back(1);
                    for (ll k = 0; k < 5; k++) {
                        a[k] = 5 - 1 - a[k];
                    }
                    for (ll k = 0; k < n; k++) {
                        a2[k] = n - 1 - a2[k];
                    }
    if (u[a] == 0) {
        dfs(a, u, t, a2);
    }
    for (ll k = 0; k < 5; k++) {
                        a[k] = 5 - 1 - a[k];
                    }
    for (ll k = 0; k < n; k++) {
                        a2[k] = n - 1 - a2[k];
                    }
    t.pop_back();

    t.push_back(2);

                    vector<ll> p(5);
                    for (ll k = 0; k < 5; k++) {
                        p[a[k]] = k;
                    }
                    a = p;
                    p.resize(n);
                    for (ll k = 0; k < n; k++) {
                        p[a2[k]] = k;
                    }
                    a2 = p;
    if (u[a] == 0) {
        dfs(a, u, t, a2);
    }
                    for (ll k = 0; k < 5; k++) {
                        p[a[k]] = k;
                    }
                    a = p;
                    p.resize(n);
                    for (ll k = 0; k < n; k++) {
                        p[a2[k]] = k;
                    }
                    a2 = p;
    t.pop_back();
}

inline void solve() {
    ll n, q;
    cin >> n >> q;
    vector<ll> a(n);
    for (ll i = 0; i < n; i++) {
        cin >> a[i];
        a[i]--;
    }
    map<vector<ll>, ll> u2;
    vector<ll> a2 = {1, 4, 0, 3, 2};
    dfs(a2, u2, {}, a);

    vector<ll> t = {1, 4, 0, 3, 2};
    for (ll qi = 0; qi < q; qi++) {
        ll x;
        cin >> x;
        x--;
            if (x == 0) {
                reverse(t.begin(), t.end());
            } else if (x == 1) {
                    for (ll k = 0; k < 5; k++) {
                        t[k] = 5 - 1 - t[k];
                    }
            } else if (x == 2) {
                    vector<ll> p(5);
                    for (ll k = 0; k < 5; k++) {
                        p[t[k]] = k;
                    }
                    t = p;
            }
        cout << ans[t] << " ";
    }
    cout << endl;
}

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