#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define f first
#define s second
#define ll long long
#define ld long double

using namespace std;

int mod = 998244353;

int binpow(int a, int n) {
    if (n == 0) return 1;
    if (n % 2 == 1) return binpow(a, n - 1) * a % mod;
    int ans = binpow(a, n / 2);
    return ans * ans % mod;
}

int get(vector<int> a, int n) {
    int sum = 0;
    for (int i = 0; i < n; i++) {
        sum += binpow(a[i], i + 1);
        sum %= mod;
    }
    return sum;
}

void solve() {
    int n, m; cin >> n >> m;
    vector<int> p(n); for (int i = 0; i < n; i++) cin >> p[i];
    vector<int> b(m); for (int i = 0; i < m; i++) cin >> b[i];
    int st = get(p, n);
    int cur = 0;
    if (b[0] == 1) {
        reverse(p.begin(), p.end());
        cur = get(p, n);
    } else if (b[0] == 2) {
        for (int i = 0; i < n; i++) p[i] = n + 1 - p[i];
        cur = get(p, n);
    } else if (b[0] == 3) {
        vector<int> q(n);
        for (int i = 0; i < n; i++) {
            q[p[i] - 1] = i + 1;
        }
        cur = get(q, n);
    }
    int cnt = 0;
    for (int i = 0; i < m; i++) {
        cnt++;
        cnt %= 2;
        if (cnt == 0) {
            cout << st << " ";
        } else {
            cout << cur << " ";
        }
    }
}

signed main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(0); cin.tie(0);
    int t = 1; // cin >> t;
    while (t--) solve();
}