#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;
using ll = long long;

const int MOD = 998244353;
int add(int a, int b) { return (a + b) % MOD; }
int mul(int a, int b) { return (1LL * a * b) % MOD; }
int bin_pow(int a, int n) {
    if (n == 0) return 1;
    if (n % 2 == 0) {
        int x = bin_pow(a, n / 2);
        return mul(x, x);
    } else {
        return mul(a, bin_pow(a, n - 1));
    }
}

int n;

vector<int> do_1(vector<int> p) {
    vector<int> q(n + 1);
    for (int i = 1; i <= n; i++) q[i] = p[n - i + 1];
    return q;
}

vector<int> do_2(vector<int> p) {
    vector<int> q(n + 1);
    for (int i = 1; i <= n; i++) q[i] = n - p[i] + 1;
    return q;
}

vector<int> do_3(vector<int> p) {
    vector<int> q(n + 1);
    for (int i = 1; i <= n; i++) q[p[i]] = i;
    return q;
}

void print(vector<int> p) {
    for (int i = 1; i <= n; i++) cout << p[i] << ' ';
    cout << '\n';
}

int cost(vector<int> p) {
    int res = 0;
    for (int i = 1; i <= n; i++) res = add(res, bin_pow(p[i], i));
    return res;
}

int main() {
    #ifdef LOCAL
    freopen("_test.in", "r", stdin);
    freopen("_test.out", "w", stdout);
    #endif

    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int q;
    cin >> n >> q;

    vector<int> p(n + 1);
    for (int i = 1; i <= n; i++) cin >> p[i];

    vector<int> b(q);
    for (int i = 0; i < q; i++) cin >> b[i];

    // do {
    //     vector<int> p_ = p;
    //     for (int i = 0; i < q; i++) {
    //         if (b[i] == 1) p_ = do_1(p_);
    //         if (b[i] == 2) p_ = do_2(p_);
    //         if (b[i] == 3) p_ = do_3(p_);
    //     }
    //     cout << cost(p_) << '\n';
    // } while (next_permutation(b.begin(), b.end()));

    int init = cost(p);
    int one = cost(do_1(p));
    int two = cost(do_2(p));
    int one_two = cost(do_2(do_1(p)));
    bool cnt_1 = false, cnt_2 = false;

    for (int i = 0; i < q; i++) {
        if (b[i] == 1) cnt_1 = !cnt_1;
        if (b[i] == 2) cnt_2 = !cnt_2;

        if (!cnt_1 && !cnt_2) {
            cout << init << ' ';
        } else if (cnt_1 && !cnt_2) {
            cout << one << ' ';
        } else if (!cnt_1 && cnt_2) {
            cout << two << ' ';
        } else if (cnt_1 && cnt_2) {
            cout << one_two << ' ';
        }
    }
}