#include<bits/stdc++.h>
// #include<ext/pb_ds/assoc_container.hpp>
// #include<ext/pb_ds/tree_policy.hpp>

// #define int long long
// #pragma GCC optimize("O3, unroll-loops")
// #pragma GCC target("avx2")

// using namespace __gnu_pbds;
using ll = long long;
using namespace std;

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

mt19937 rnd(2007);
const int MOD = 998244353;

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

int get(int n, vector<int> &p) {
    int ans = 0;
    for (int i = 0; i < n; i++) {
        ans += binpow(p[i], i + 1);
        ans %= MOD;
    }
    return ans;
}

void f1(int n, vector<int> &p) {
    reverse(p.begin(), p.end());
}

void f2(int n, vector<int> &p) {
    vector<int> q(n);
    for (int j = 0; j < n; j++) {
        q[j] = n - p[j] + 1;
    }
    p = q;
}

void f3(int n, vector<int> &p) {
    vector<int> q(n);
    for (int j = 0; j < n; j++) {
        q[p[j] - 1] = j + 1;
    }
    p = q;
}

void solve() {
    int n, q;
    cin >> n >> q;
    vector<int> ans(8), p(n);
    for (int i = 0; i < n; i++) {
        cin >> p[i];
    }
    for (int mask = 0; mask < 8; mask++) {
        vector<int> q = p;
        if (mask & (1 << 0)) {
            f1(n, q);
        }
        if (mask & (1 << 1)) {
            f2(n, q);
        }
        if (mask & (1 << 2)) {
            f3(n, q);
        }
        ans[mask] = get(n, q);
    }
    int mask = 0;
    for (int i = 0; i < q; i++) {
        int t;
        cin >> t;
        if (t == 1) {
            mask ^= (1 << 0);
        } else if (t == 2) {
            mask ^= (1 << 1);
        } else {
            mask ^= (1 << 2);
        }
        cout << ans[mask] << " ";
    }
    cout << "\n";
}

signed main() {
    int t = 1;
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    // cin >> t;
#else
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
#endif
    // cin >> t;
    while (t--) {
        solve();
    }
}