#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;

const ll MOD = 998244353;

ll powmod(ll a, ll e) {
    ll res = 1;

    while (e > 0) {
        if (e & 1) {
            res = res * a % MOD;
        }
        e >>= 1;
        a = a * a % MOD;
    }

    return res;
}

void solve() {
    ll n, k, t;
    cin >> n >> k;
    vector<ll> p(n);

    for (ll& l : p) {
        cin >> l;
    }

    array<ll, 2> ans = {};

    for (ll i = 0; i < n; i++) {
        ans[0] = (ans[0] + powmod(p[i], i + 1)) % MOD;
    }

    for (ll i = 0; i < k; i++) {
        cin >> t;
    }
    vector<ll> q(n);
    switch (t) {
        case 1:
            for (ll i = 0; i < n; i++) {
                q[i] = p[n - i - 1];
            }
            break;
        case 2:
            for (ll i = 0; i < n; i++) {
                q[i] = n - p[i] + 1;
            }
            break;
        case 3:
            for (ll i = 0; i < n; i++) {
                q[p[i] - 1] = i + 1;
            }
            break;
    }
    for (ll i = 0; i < n; i++) {
        ans[1] = (ans[1] + powmod(p[i], i + 1)) % MOD;
    }

    for (ll i = 1; i <= k; i++) {
        cout << ans[i % 2] << ' ';
    }

    cout << endl;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    ll t = 1;
#ifdef LOCAL
    freopen("in_b.txt", "r", stdin);
    cin >> t;
#endif

    while (t--) solve();
}