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

#define all(a) (a).begin(), (a).end()

using namespace std;
using namespace __gnu_pbds;

using ll = long long;
using ld = long double;

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

void solve();

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t = 1;
    //cin >> t;
    while (t--) solve();
}

#define int ll

int mod = 998244353;

struct Mint {
    int x;

    Mint() :x(0) {}
    Mint(int x) : x(x) {}
};

Mint operator+(Mint a, Mint b) {
    int c = a.x + b.x;
    return c < mod ? c : c - mod;
}

Mint operator*(Mint a, Mint b) {
    return a.x * b.x % mod;
}

Mint powm(Mint a, int n) {
    if (n == 0) return 1;
    Mint t = powm(a, n >> 1);
    t = t * t;
    return n & 1 ? t * a : t;
}

void solve() {
    int n, q;
    cin >> n >> q;
    vector<int> p(n);
    for (auto& x : p) {
        cin >> x; --x;
    }
    vector<int> q1(n), q2(n), q3(n), q4(n), q5(n), q6(n), q7(n);
    for (int i = 0; i < n; ++i) q1[i] = p[n - i - 1];
    for (int i = 0; i < n; ++i) q2[i] = n - p[i] - 1;
    for (int i = 0; i < n; ++i) q3[i] = n - q1[i] - 1;
    for (int i = 0; i < n; ++i) q4[p[i]] = i;
    for (int i = 0; i < n; ++i) q5[q1[i]] = i;
    for (int i = 0; i < n; ++i) q6[q2[i]] = i;
    for (int i = 0; i < n; ++i) q7[q3[i]] = i;
    Mint s = 0, s1 = 0, s2 = 0, s3 = 0, s4 = 0, s5 = 0, s6 = 0, s7 = 0;
    for (int i = 0; i < n; ++i) s = s + powm(p[i] + 1, i + 1);
    for (int i = 0; i < n; ++i) s1 = s1 + powm(q1[i] + 1, i + 1);
    for (int i = 0; i < n; ++i) s2 = s2 + powm(q2[i] + 1, i + 1);
    for (int i = 0; i < n; ++i) s3 = s3 + powm(q3[i] + 1, i + 1);
    for (int i = 0; i < n; ++i) s4 = s4 + powm(q4[i] + 1, i + 1);
    for (int i = 0; i < n; ++i) s5 = s5 + powm(q5[i] + 1, i + 1);
    for (int i = 0; i < n; ++i) s6 = s6 + powm(q6[i] + 1, i + 1);
    for (int i = 0; i < n; ++i) s7 = s7 + powm(q7[i] + 1, i + 1);
    int i = 0;
    int mask = 0;
    while (q--) {
        int t;
        cin >> t;
        if (t == 1) mask ^= 1;
        if (t == 2) mask ^= 2;
        if (t == 3) mask ^= 4;
        if (mask == 0) cout << s.x << ' ';
        if (mask == 1) cout << s1.x << ' ';
        if (mask == 2) cout << s2.x << ' ';
        if (mask == 3) cout << s3.x << ' ';
        if (mask == 4) cout << s4.x << ' ';
        if (mask == 5) cout << s5.x << ' ';
        if (mask == 6) cout << s6.x << ' ';
        if (mask == 7) cout << s7.x << ' ';
    }
    cout << '\n';
}