#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;
template<class T>
using oset = tree<T, null_type, std::less<T>, rb_tree_tag, tree_order_statistics_node_update>;
using ll = long long;
ll MOD = 998244353;

vector<ll> rev(vector<ll> p) {
    reverse(p.begin(), p.end());
    return p;
}

vector<ll> raz(vector<ll> p) {
    for (auto &i : p) i = p.size() - i + 1;
    return p;
}

vector<ll> obr(vector<ll> p) {
    vector<ll> q(p.size());
    for (ll i = 0;i < p.size();i++) q[p[i] - 1] = i + 1;
    return q;
}

ll mul(ll a, ll b) {
    return a * b % MOD;
}

ll bp(ll a, ll b) {
    if (b == 0) return 1;
    if (b == 1) return a;
    if (b % 2 == 0) {
        ll x = bp(a, b / 2);
        return mul(x, x);
    }
    else {
        return mul(a, bp(a, b - 1));
    }
}

ll calc(vector<ll> p) {
    ll sum = 0;
    for (ll i = 0;i < p.size();i++) {
        sum = (sum + bp(p[i], i + 1)) % MOD;
    }
    return sum;
}

void viperr() {
    ll n, q;
    cin >> n >> q;
    map<vector<ll>, string> s;
    map<string, ll> sum;
    map<string, string> soot;
    map<string, bool> used;
    vector<ll> p(n);
    for (auto &i : p) cin >> i;
    vector<ll> b(q);
    for (auto &i : b) cin >> i;
    soot[""] = "";
    soot["1"] = "1";
    soot["2"] = "2";
    soot["12"] = "12";
    soot["11"] = "";
    soot["22"] = "";
    soot["21"] = "12";
    soot["121"] = "2";
    soot["122"] = "1";
    s[p] = "";
    s[raz(p)] = "2";
    s[rev(p)] = "1";
    s[raz(rev(p))] = "12";
    sum["12"] = calc(raz(rev(p)));
    sum["2"] = calc(raz(p));
    sum["1"] = calc(rev(p));
    sum[""] = calc(p);
    ll c = 1 << 4;
    for (ll mask = 0;mask < c;mask++) {
        string op;
        if (mask & 1) {
            op.push_back('1');
        }
        if ((mask >> 1) & 1) {
            op.push_back('2');
        }
        op.push_back('3');
        if ((mask >> 2) & 1) {
            op.push_back('1');
        }
        if ((mask >> 3) & 1) {
            op.push_back('2');
        }
        vector<ll> a = p;
        for (ll i = 0;i < op.size();i++) {
            if (op[i] == '1') a = rev(a);
            else if (op[i] == '2') a = raz(a);
            else a = obr(a);
        }
        soot[op] = op;
        sum[op] = calc(a);
    }
    for (ll mask = 0;mask < c;mask++) {
        string op;
        if (mask & 1) {
            op.push_back('1');
        }
        if ((mask >> 1) & 1) {
            op.push_back('2');
        }
        op.push_back('3');
        if ((mask >> 2) & 1) {
            op.push_back('1');
        }
        if ((mask >> 3) & 1) {
            op.push_back('2');
        }
        op.push_back('3');
        vector<ll> a = p;
        for (ll i = 0;i < op.size();i++) {
            if (op[i] == '1') a = rev(a);
            else if (op[i] == '2') a = raz(a);
            else a = obr(a);
        }
        string res = s[a];
        soot[op] = res;
    }
    string now;
    ll cnt1 = 0, cnt2 = 0;
    vector<ll> ans;
    for (ll i = 0;i < q;i++) {
        if (b[i] == 1) {
            cnt1++;
            if (cnt1 % 2) now.push_back('1');
            if (cnt2 % 2) now.push_back('2');
            ans.push_back(sum[now]);
            if (cnt1 % 2) now.pop_back();
            if (cnt2 % 2) now.pop_back();
        }
        else if (b[i] == 2) {
            cnt2++;
            if (cnt1 % 2) now.push_back('1');
            if (cnt2 % 2) now.push_back('2');
            ans.push_back(sum[now]);
            if (cnt1 % 2) now.pop_back();
            if (cnt2 % 2) now.pop_back();
        }
        else {
            if (cnt1 % 2) now.push_back('1');
            if (cnt2 % 2) now.push_back('2');
            now.push_back('3');
            cnt1 = 0;
            cnt2 = 0;
            now = soot[now];
            ans.push_back(sum[now]);
        }
    }
    for (auto i : ans) cout << i << ' ';
}

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