#include <bits/stdc++.h>
#define int long long

#pragma GCC optimize("Ofast","O3","unroll-loops")
#pragma GCC target("avx2")

using namespace std;
const int MOD = 998'244'353;

int binpow(int x, int m) {
    if (m == 0)
        return 1;
    if (m == 1)
        return x;
    if (m % 2 == 0) {
        int res = binpow(x, m / 2);
        return (res * res) % MOD;
    }
    return (binpow(x, m - 1) * x) % MOD;
}

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

vector<int> f(vector<int> v, vector<int> ops) {
    for (auto t : ops) {
        if (t == 1) {
            reverse(v.begin(), v.end());
        }
        if (t == 2) {
            for (auto &i : v)
                i = n + 1 - i;
        }
        if (t == 3) {
            vector<int> res(n);
            for (int i = 0; i < n; ++i)
                res[v[i] - 1] = i + 1;
            swap(res, v);
        }
    }
    return v;
}

int cost(vector<int> v) {
    int res = 0;
    for (int i = 0; i < v.size(); ++i) {
        int add = binpow(v[i], i + 1);
        res += add;
        res %= MOD;
    }
    return res;
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    cin >> n >> q;
    p.resize(n);
    b.resize(q);
    for (auto &i : p)
        cin >> i;
    for (auto &i : b)
        cin >> i;

    map<vector<int>, int> mp;
    vector<int>
        v123 = { 1, 2, 3 },
        v12 = { 1, 2 },
        v13 = { 1, 3 },
        v23 = { 2, 3 },
        v1 = { 1 },
        v2 = { 2 },
        v3 = { 3 },
        v0 = { 0 };
    vector<vector<int>> vars = { v1, v2, v3,
                                v12, v23, v13,
                                v123 };
    for (auto i : vars) {
        auto v = f(p, i);
        mp[i] = cost(v);
    }
    mp[v0] = cost(p);

    vector<int> cur = v0;
    for (int i = 0; i < q; ++i) {
        vector<int> key;
        if (cur == v123) {
            if (b[i] == 1) {
                key = v13;
            }
            else if (b[i] == 2) {
                key = v123;
            }
            else if (b[i] == 3) {
                key = v12;
            }
        }
        else if (cur == v12) {
            if (b[i] == 1) {
                key = v2;
            }
            else if (b[i] == 2) {
                key = v1;
            }
            else if (b[i] == 3) {
                key = v123;
            }
        }
        else if (cur == v13) {
            if (b[i] == 1) {
                key = v123;
            }
            else if (b[i] == 2) {
                key = v3;
            }
            else if (b[i] == 3) {
                key = v1;
            }
        }
        else if (cur == v23) {
            if (b[i] == 1) {
                key = v3;
            }
            else if (b[i] == 2) {
                key = v123;
            }
            else if (b[i] == 3) {
                key = v2;
            }
        }
        else if (cur == v1) {
            if (b[i] == 1) {
                key = v0;
            }
            else if (b[i] == 2) {
                key = v12;
            }
            else if (b[i] == 3) {
                key = v13;
            }
        }
        else if (cur == v2) {
            if (b[i] == 1) {
                key = v12;
            }
            else if (b[i] == 2) {
                key = v0;
            }
            else if (b[i] == 3) {
                key = v23;
            }
        }
        else if (cur == v3) {
            if (b[i] == 1) {
                key = v23;
            }
            else if (b[i] == 2) {
                key = v13;
            }
            else if (b[i] == 3) {
                key = v0;
            }
        }
        else {
            if (b[i] == 1) {
                key = v1;
            }
            else if (b[i] == 2) {
                key = v2;
            }
            else if (b[i] == 3) {
                key = v3;
            }
        }
        if (mp[key] <= 0 || mp[key] >= MOD) {
            return -1;
        }
        cout << mp[key] << " ";
        cur = key;
    }

}
