#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();
    bool f = 0;
    int cost = 0;
    if (b[1] == 1) {
        reverse(p + 1, p + 1 + n);
        for (int i = 1; i <= n; i++) {
            cost += bin_pow(p[i], i);
            if (cost >= MOD) cost -= MOD;
        }
        for (int i = 1; i <= q; i++) cout << cost << ' ';
    }
    else if (b[1] == 3) {

        while (true) {

        }
        vector <int> Q(n  + 1);
        for (int i = 1; i <= n; i++) Q[p[i]] = i;
        for (int i = 1; i <= n; i++) p[i] = Q[i];
        for (int i = 1; i <= n; i++) {
            cost += bin_pow(p[i], i);
            if (cost >= MOD) cost -= MOD;
        }
        cout << cost << ' ';
        cost = 0;
        for (int i = 1; i <= n; i++) {
            cost += bin_pow(i, i);
            if (cost >= MOD) cost -= MOD;
        }
        for (int i = 2; i <= q; i++) cout << cost << ' ';
    }
    else {
        int cost2 = 0;
        for (int i = 1; i <= n; i++) {
            cost += bin_pow(p[i], i);
            if (cost >= MOD) cost -= MOD;
        }
        for (int i = 1; i <= n; i++) p[i] = n - p[i] + 1;
        for (int i = 1; i <= n; i++) {
            cost2 += bin_pow(p[i], i);
            if (cost2 >= MOD) cost -= MOD;
        }
        for (int i = 1; i <= q; i++) {
            cout << (i % 2 ? cost2 : cost) << ' ';
        }
    }
}

int32_t main() {
    SLAVA_MORDOVII();
}