#include <bits/stdc++.h>

#ifndef LOC
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#define debug(x)
#else
#define debug(x) cout << __LINE__ << ": " << (#x) << " = " << (x) << endl;
#endif

using namespace std;

typedef long long ll;

template <typename T>
ostream& operator<< (ostream& out, vector<T> v) {
    for (int i = 0; i < v.size(); i++) {
        out << v[i] << ' ';
    }
    return out;
}

const ll MOD = 998244353;

ll bpow(ll a, ll p) {
    if (p == 0) return 1;
    ll t = bpow(a, p/2);

    return (t * t % MOD) * (p % 2 ? a : 1) % MOD;
}

ll ans(vector<int>& a) {
    ll res = 0;
    for (int i = 0; i < a.size(); i++) {
        res = (res + bpow(a[i], i + 1)) % MOD;
    }
    return res;
}

void solve() {
    int n, q; cin >> n >> q;

    vector<int> p(n);
    for (int& i : p) cin >> i;

    bool t4 = true;

    for (int i = 0; i < n; i++) {
        if (p[i] != i + 1) {
            t4 = false;
            break;
        }
    }

    vector<int> bv(q);
    for (int& i : bv) cin >> i;

    bool t2 = true;
    for (int i = 1; i < q; i++) {
        if (bv[i] != bv[0]) {
            t2 = false;
            break;
        }
    }

    if (t4) {
        ll usual = ans(p);
        reverse(p.begin(), p.end());
        ll rev = ans(p);
        
        bool isrev = false;
        for (int& b : bv) {
            if (b == 1) {
                isrev = !isrev;
            } else if (b == 2) {
                isrev = !isrev;
            } else {
                isrev = isrev;
            }

            cout << (isrev ? rev : usual) << ' ';
        }
        return;
    }

    if (t2) {
        ll usual = ans(p);
        if (bv[0] == 1) {
            reverse(p.begin(), p.end());
        } else if (bv[0] == 2) {
            for (int i = 0; i < n; i++) {
                p[i] = n - p[i] + 1;
            }
        } else {
            vector<int> q(n);
            for (int i = 0; i < n; i++) {
                q[p[i]-1] = i + 1;
            }
            p = q;
        }
        ll rev = ans(p);

        for (int i = 0; i < q; i++) {
            cout << (i % 2 ? usual : rev) << ' ';
        }
    }

    for (int& b : bv) {
        if (b == 1) {
            reverse(p.begin(), p.end());
        } else if (b == 2) {
            for (int i = 0; i < n; i++) {
                p[i] = n - p[i] + 1;
            }
        } else {
            vector<int> q(n);
            for (int i = 0; i < n; i++) {
                q[p[i]-1] = i + 1;
            }
            p = q;
        }

        cout << ans(p) << ' ';
        return;
    }
}

signed main() {
    #ifndef LOC
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(0);
    #endif

    int t = 1;
    //cin>>t;
    
    while(t--) solve();
}
