// #pragma GCC optimize("O3,fast-math,unroll-loops")
#include <bits/stdc++.h>

#define i64 int64_t
#define vi vector<int>
#define vvi vector<vi>
#define vc vector
#define vb vc<bool>
#define pb push_back
#define pii pair<int, int>
#define fi first
#define se second
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()

using namespace std;

template <typename T>

istream& operator>>(istream& in, vector<T> &a) {
    for (T &el : a) in >> el;
    return in;
}

template <typename T>

ostream& operator<<(ostream& out, vector<T> a) {
    for (T &el : a) out << el << " ";
    return out;
}
#define int i64

const int MOD = 998244353;

int pw(int a, int b) {
    if (!b) return 1;
    if (b & 1) return 1ll * pw(a, b - 1) * a % MOD;
    return pw(1ll * a * a % MOD, b / 2);
}


void solve() {
    int n, q;
    cin >> n >> q;
    vi p(n);
    cin >> p;
    vi b(q);
    cin >> b;
    int x = 0;
    bool is1 = 0, is2 = 0, is3 = 0;
    vi mp(8, -1);
    for (int i = 0; i < q; ++i) {
        if (b[i] == 1) {
            // reverse(all(p));
            is1 ^= 1;
        } else if (b[i] == 2) {
            // for (int &el : p) el = n - el + 1;
            is2 ^= 1;
        } else {
            // vi q(n);
            // for (int i = 0; i < n; ++i) {
            //     q[p[i] - 1] = i + 1;
            // }
            // p = q;
            is3 ^= 1;
            swap(is1, is2);
        }
        if (mp[is1 * 4 + is2 * 2 + is3] != -1) {
            cout << mp[is1 * 4 + is2 * 2 + is3] << ' ';
            continue;
        }
        if (is1) {
            reverse(all(p));
        }
        if (is2) {
            for (int &el : p) el = n - el + 1;
        }
        if (is3) {
            vi q(n);
            for (int i = 0; i < n; ++i) {
                q[p[i] - 1] = i + 1;
            }
            p = q;
        }
        int ans = 0;
        for (int j = 0; j < n; ++j) {
            ans += pw(p[j], j + 1);
            ans %= MOD;
        }
        mp[is1 * 4 + is2 * 2 + is3] = ans;
        cout << ans << ' ';

        if (is3) {
            vi q(n);
            for (int i = 0; i < n; ++i) {
                q[p[i] - 1] = i + 1;
            }
            p = q;
        }

        if (is2) {
            for (int &el : p) el = n - el + 1;
        }

        if (is1) {
            reverse(all(p));
        }
    }
}

int32_t main() {
    // freopen("inputb.txt", "r", stdin);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    solve();
}