#include <bits/stdc++.h>
using namespace std;

#pragma GCC optimize("03")
#pragma GCC optimize("unroll-loops")

#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define int long long

using pii = pair <int, int>;

const int mod = 998244353;

int bin_pow(int x, int n) {
    if (n == 0) return 1 % mod;
    if (n % 2 == 0) return bin_pow(x * x % mod, n / 2);
    return x * bin_pow(x, n - 1) % mod;
}

void add(int &x, int y) {
    x += y;
    if (x >= mod) x -= mod;
}

void del(int &x, int y) {
    x -= y;
    if (x < 0) x += mod;
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n, q; cin >> n >> q;
    vector <int> a(n), b(q);
    for (int i = 0; i < n; ++i) cin >> a[i];
    for (int i = 0; i < q; ++i) cin >> b[i];
    int ans = 0;
    for (int i = 0; i < n; ++i) add(ans, bin_pow(a[i], i + 1));
    if (count(all(b), 1) == q) {
        int ans1 = 0;
        for (int i = 0; i < n; ++i) add(ans1, bin_pow(a[n - i - 1], i + 1));
        for (int i = 0; i < q; ++i) {
            cout << (i % 2 == 0 ? ans1 : ans) << '\n';
        }
        return 0;
    }
    if (count(all(b), 2) == q) {
        int ans1 = 0;
        for (int i = 0; i < n; ++i) add(ans1, bin_pow(n - a[i] + 1, i + 1));
        for (int i = 0; i < q; ++i) {
            cout << (i % 2 == 0 ? ans1 : ans) << '\n';
        }
        return 0;
    }
    if (count(all(b), 3) == q) {
        vector <int> a1(n);
        for (int i = 0; i < n; ++i) {
            a1[a[i] - 1] = i + 1;
        }
        int ans1 = 0;
        for (int i = 0; i < n; ++i) add(ans1, bin_pow(a1[i], i + 1));
        for (int i = 0; i < q; ++i) {
            cout << (i % 2 == 0 ? ans1 : ans) << '\n';
        }
        return 0;
    }
    for (int i = 0; i < q; ++i) {
        if (b[i] == 1) {
            for (int j = 0; j < n; ++j) {
                del(ans, bin_pow(a[j], j + 1));
                add(ans, bin_pow(a[n - j - 1], j + 1));
            }
            reverse(all(a));
        } else if (b[i] == 2) {
            for (int j = 0; j < n; ++j) {
                del(ans, bin_pow(a[j], j + 1));
                a[j] = n - a[j] + 1;
                add(ans, bin_pow(a[j], j + 1));
            }
        } else {
            vector <int> p(n);
            for (int j = 0; j < n; ++j) {
                del(ans, bin_pow(a[j], j + 1));
                p[a[j] - 1] = j + 1;
            }
            a = p;
            for (int j = 0; j < n; ++j) {
                add(ans, bin_pow(a[j], j + 1));
            }
        }
        cout << ans << ' ';
    }

}