#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;
using ll = long long;

template <class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

const ll mod = 998'244'353;
const int maxn = 1e5 + 5;

ll binpow(ll a, ll b) {
    ll res = 1;
    for (int i = 0; i < 25; i++, a = a * a % mod) {
        if ((b >> i) & 1) {
            res = res * a % mod;
        }
    }
    return res;
}

ll ps[2][2][2][maxn];
ll anss[2][2][2];

void dfs(vector <int> mods, int ind, vector <int> p) {
    if (ind == 3) {
        ll res = 0;
        for (int i = 0; i < p.size(); i++) {
            res = (res + binpow(p[i], i+1)) % mod;
            ps[mods[0]][mods[1]][mods[2]][i] = p[i];
        }

        // if (mods[0] == 0 && mods[1] == 1 && mods[2] == 1) {
        //     for (auto i : p) {
        //         cout << i << ' ';
        //     }
        //     cout << '\n';
        // }
        anss[mods[0]][mods[1]][mods[2]] = res;
        return;
    }
    dfs(mods, ind+1, p);

    if (ind == 1) {
        reverse(p.begin(), p.end());
        mods[ind] = 1;
        dfs(mods, ind+1, p);
    } else if (ind == 2) {
        for (auto &i : p) {
            i = p.size() - i + 1;
        }
        mods[ind] = 1;
        dfs(mods, ind+1, p);
    } else {
        auto p1 = p;
        for (int i = 0; i < p.size(); i++) {
            p[p1[i]-1] = i+1;
        }
        mods[ind] = 1;
        dfs(mods, ind+1, p);
    }
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int n, q;
    cin >> n >> q;

    vector <int> p(n), b(q);

    for (int i = 0; i < n; i++) {
        cin >> p[i];
    }
    
    for (int i = 0; i < q; i++) {
        cin >> b[i];
    }

    vector <int> mods(3);
    dfs(mods, 0, p);

    bool t1 = 0, t2 = 0, t3 = 0;

    for (auto cm : b) {
        if (cm == 1) {
            t2 = !t2;
        } else if (cm == 2) {
            t3 = !t3;
        } else {
            t1 = !t1;
        }

        cout << anss[t1][t2][t3] << ' ';

        // for (auto i : p) {
        //     cout << i << ' ';
        // }
        // cout << '\n';

        // ll res = 0;
        // for (int i = 0; i < n; i++) {
        //     res = (res + binpow(p[i], i+1)) % mod;
        // }
        // cout << res << ' ';
    }

    // for (int i = 0; i < n / 2; i++) {
    //     swap(p[i], p[n-i-1]);
    // }
    // for (auto &i : p) {
    //     i = n - i + 1;
    // }
    // vector <int> p1 = p;
    // for (int i = 0; i < n; i++) {
    //     p[p1[i]-1] = i+1;
    // }
    // for (int i = 0; i < n / 2; i++) {
    //     swap(p[i], p[n-i-1]);
    // }

    // cout << '\n';
    // for (auto i : p) {
    //     cout << i << ' ';
    // }
}
