#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define ld long double
#define pb push_back
#define f first
#define s second
#define all(x) x.begin(), x.end()
#define ub upper_bound
#define lb lower_bound
#define mp make_pair
#define pll pair<ll, ll>

mt19937_64 rnd(chrono::steady_clock::now().time_since_epoch().count());

const ll mod = 998244353;

ll b_pow(ll n, ll st) {
    ll ans = 1;
    while (st > 0) {
        if (st % 2) {
            ans *= n;
            ans%=mod;
            st--;
        }
        else {
            st/=2;
            n*=n;
            n%=mod;
        }
    }
    return ans;
}

ll sum = 0;
ll q[100100];

ll n;

vector<ll> ch1(vector<ll> p) {
    vector<ll> b = p;
    reverse(all(b));
    return b;
}

vector<ll> ch2(vector<ll> p) {
    vector<ll> b = p;
    for (int i = 0; i < n; i++) {
        b[i] = n - b[i] + 1;
    }
    return b;
}

vector<ll> ch3(vector<ll> p) {
    vector<ll> b(n);
    for (int i = 0; i < n; i++) {
        b[p[i] - 1] = i + 1;
    }
    return b;
}

map<pair<pll, int>, pair<pll, ll>> al;
map<pll, vector<pair<int, pll>>> g;

pll to_hash(vector<ll> p) {
    pll ans = {0, 0};
    ll p1 = 127;
    ll p2 = 1237;
    ll md = 1e9 + 9;
    for (int i = 0; i < p.size(); i++) {
        ans.f *= p1;
        ans.f %= md;
        ans.f += p[i];
        ans.f %= md;
        ans.s*=p2;
        ans.s%=mod;
        ans.s += p[i];
        ans.s%=mod;
    }
    return ans;
}

map<pll, bool> used;

vector<ll> dfs(pll nw, pll need, vector<ll> b) {
    used[nw] = 1;
    if (nw == need)
        return b;
    for (auto nw : g[nw]) {
        ll op = nw.f;
        pll hs = nw.s;
        if (used[hs])
            continue;
        if (op == 1)
            b = ch1(b);
        if (op == 2)
            b = ch2(b);
        if (op == 3)
            b = ch3(b);
        vector<ll> an = dfs(hs, need, b);
        if (op == 1)
            b = ch1(b);
        if (op == 2)
            b = ch2(b);
        if (op == 3)
            b = ch3(b);
        if (an[0] != -1)
            return an;
    }
    return {-1};
}

vector<ll> p;

void solve() {
    ll m;
    cin >> n >> m;
    p.resize(n);
    for (int i = 0; i < n; i++) {
        cin >> p[i];
    }
    vector<ll> cp = p;
    pll st = to_hash(p);
    pll hs = st;
    for (int i = 0; i < m; i++) {
        cin >> q[i];
        ll ans = 0;
        if (al.find(mp(hs, q[i])) == al.end()) {
            vector<ll> b = dfs(st, hs, p);
            if (q[i] == 1)
                b = ch1(b);
            if (q[i] == 2)
                b = ch2(b);
            if (q[i] == 3)
                b = ch3(b);
            ans = 0;
          // cout << endl;
           // cout << "B: ";
            for (int i = 0; i < b.size(); i++) {
                ans += b_pow(b[i], i + 1);
                ans %= mod;
            //    cout << b[i] << ' ';
            }
          //  cout << endl;
           // cout << endl;
            al[mp(hs, q[i])] = mp(to_hash(b), ans);
            g[hs].pb({q[i], to_hash(b)});
            used.clear();
        }
        cout << al[mp(hs, q[i])].s << ' ';
        hs = al[mp(hs, q[i])].f;
    }
}

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    ll t = 1;
    //cin >> t;
    while (t--) {
        solve();
        cout << '\n';
    }
    return 0;
}
