#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#define int long long
#define pll pair <int, int>
#define f first
#define s second
#define all(A) A.begin(), A.end()
#define rall(A) A.rbegin(), A.rend()
#define oset ordered_set
#define omultiset ordered_multiset
#define skip continue
#pragma GCC target("avx2")
#pragma GCC optmize("O3")
#pragma GCC optmize("unroll-loops")

using namespace std;
using namespace __gnu_pbds;

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

const int N = 5e5 + 5, MOD = 998244353;
int p[N], b[N];

int bin_pow(int x, int n) {
    int res = 1;
    while (n) {
        if ((n & 1)) res = res * x % MOD;
        n >>= 1;
        x = x * x % MOD;
    }
    return res;
}

int scan() {
    int x; cin >> x;
    return x;
}

string read() {
    string s; cin >> s;
    return s;
}

void SLAVA_MORDOVII() {
    int n = scan(), q = scan();
    vector <int> Q(n + 1);
    for (int i = 0; i < n; i++) p[i + 1] = scan();
    for (int i = 0; i < q; i++) b[i + 1] = scan();
    int sum1 = 0, sum2 = 0, sum3 = 0, aft2_sum3 = 0;
    for (int i = 1; i <= n; i++) {
        sum1 += bin_pow(p[i], i);
        if (sum1 >= MOD) sum1 -= MOD;
        sum2 += bin_pow(n + 1 - p[i], i);
        if (sum2 >= MOD) sum2 -= MOD;
        sum3 += bin_pow(i, p[i]);
        if (sum3 >= MOD) sum3 -= MOD;
        aft2_sum3 += bin_pow(i, n + 1 - p[i]);
        if (aft2_sum3 >= MOD) aft2_sum3 -= MOD;
    }
    int rev_sum1 = 0, rev_sum2 = 0, rev_sum3 = 0, aft2_rev_sum3 = 0;
    reverse(p + 1, p + 1 + n);
    int cnt_rev = 0;
    for (int i = 1; i <= n; i++) {
        rev_sum1 += bin_pow(p[i], i);
        if (rev_sum1 >= MOD) rev_sum1 -= MOD;
        rev_sum2 += bin_pow(n + 1 - p[i], i);
        if (rev_sum2 >= MOD) rev_sum2 -= MOD;
        rev_sum3 += bin_pow(i, p[i]);
        if (sum3 >= MOD) sum3 -= MOD;
        aft2_rev_sum3 += bin_pow(i, n + 1 - p[i]);
        if (aft2_rev_sum3 >= MOD) aft2_rev_sum3 -= MOD;
    }
    //cout << sum1 << ' ' << sum2 << ' ' << sum3 << ' ' << aft2_sum3 << "    " << rev_sum1 << ' ' << rev_sum2 << ' ' << rev_sum3 << ' ' << aft2_rev_sum3 << '\n';
    for (int i = 1; i <= q; i++) {
        if (b[i] == 1) {
            swap(sum1, rev_sum1);
            swap(sum2, rev_sum2);
            swap(sum3, rev_sum3);
            swap(aft2_rev_sum3, aft2_sum3);
            cout << sum1 << ' ';
        }
        else if (b[i] == 2) {
            cout << sum2 << ' ';
            swap(sum2, sum1);
            swap(rev_sum2, rev_sum1);
            swap(sum3, aft2_sum3);
            swap(rev_sum3, aft2_rev_sum3);
        }
        else {
            cout << sum3 << ' ';
            swap(sum1, sum3);
            swap(rev_sum1, rev_sum3);
            swap(sum2, aft2_sum3);
            swap(rev_sum2, aft2_rev_sum3);
        }
    }
}

int32_t main() {
    SLAVA_MORDOVII();
}