#include <bits/stdc++.h>

using namespace std;
using ll = long long;
ll MOD = 998244353;

ll moded(ll a) {
    return (a % MOD + MOD) % MOD;
}

ll binpow(ll a, ll n) {
    if (n == 0)
        return 1ll;
    if (n % 2 == 0)
        return binpow(moded(a * a), n / 2);
    return moded(binpow(a, n - 1) * a);
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, q;
    cin >> n >> q;
    vector <ll> p(n);
    bool group1 = (n <= 1000 && q <= 1000),
        group2 = true,
        group3 = true,
        group4 = true;
    for (int i = 0; i < n; ++i) {
        cin >> p[i];
        if (p[i] != i + 1)
            group4 = false;
    }
    vector <int> qrs(q);
    for (int i = 0; i < q; ++i) {
        cin >> qrs[i];
        if (i > 0 && qrs[i] != qrs[i - 1])
            group2 = false;
        if (qrs[i] == 3)
            group3 = false;
    }
    auto cost = [&]() {
        ll ans = 0;
        for (int i = 0; i < n; ++i)
            ans = moded(ans + binpow(p[i], i + 1));
        return ans;
    };
    auto op1 = [&]() {
        for (int i = 0; 2 * i + 1 < n; ++i)
            swap(p[i], p[n - i - 1]);
    };
    auto op2 = [&]() {
        for (int i = 0; i < n; ++i)
            p[i] = n - p[i] + 1;
    };
    auto op3 = [&]() {
        vector <ll> new_p(n);
        for (int i = 0; i < n; ++i)
            new_p[p[i] - 1] = i + 1;
        p = new_p;
    };
    if (group1) {
        for (int type : qrs) {
            if (type == 1) op1();
            else if (type == 2) op2();
            else op3();
            cout << cost() << " ";
        }
        cout << "\n";
        return 0;
    }
    if (group2) {
        int type = qrs.back();
        ll ans1 = cost();
        if (type == 1) op1();
        else if (type == 2) op2();
        else op3();
        ll ans2 = cost();
        for (int i = 0; i < q; ++i)
            cout << (i % 2 ? ans1 : ans2) << " ";
        cout << "\n";
        return 0;
    }
    if (group3) {
        bool c1 = false, c2 = false;
        ll ans[2][2];
        ans[0][0] = cost();
        op1();
        ans[1][0] = cost();
        op2();
        ans[1][1] = cost();
        op1();
        ans[0][1] = cost();
        for (auto type : qrs) {
            if (type == 1) c1 ^= true;
            else c2 ^= true;
            cout << ans[c1][c2] << " ";
        }
        cout << "\n";
        return 0;
    }
    if (group4) {
        ll ans[2];
        ans[0] = cost();
        op1();
        ans[1] = cost();
        bool rev = false;
        for (int type : qrs) {
            if (type != 3)
                rev ^= true;
            cout << ans[rev] << " ";
        }
        cout << "\n";
        return 0;
    }
    cout << "gg\n";
    return 0;
}
