#include <bits/stdc++.h>
#define int long long
#define pb piush_back
#define ins insert

using namespace std;

const int mod = 998244353;

int32_t main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n, m, i, j, k, k1, k2, k3, x, x1, x2, x3, t, g;
    cin >> n >> t;
    vector <int> a(n);
    for (auto &it : a)
        cin >> it;
    vector <int> b(t);
    for (auto &it : b)
        cin >> it;
    k1 = 0;
    for (i = 0; i < n; i++) {
        x = 1;
        k3 = i + 1;
        x1 = a[i];
        while(k3 != 0) {
            if (k3 % 2 == 1) {
                k3--;
                x = (x * x1) % mod;
            }
            else {
                k3/=2;
                x1 = (x1 * x1) % mod;
            }
        }
        k1 = (k1 + x) % mod;
    }
    x = b[0];
    if (x == 1)
        reverse(a.begin(), a.end());
    else if (x == 2)
        for (i = 0; i < n; i++)
            a[i] = n - a[i] + 1;
    else {
        vector <int> c;
        c = a;
        for (i = 0; i < n; i++)
            a[c[i] - 1] = i + 1;
    }
    k2 = 0;
    for (i = 0; i < n; i++) {
        x = 1;
        k3 = i + 1;
        x1 = a[i];
        while(k3 != 0) {
            if (k3 % 2 == 1) {
                k3--;
                x = (x * x1) % mod;
            }
            else {
                k3/=2;
                x1 = (x1 * x1) % mod;
            }
        }
        k2 = (k2 + x) % mod;
    }
    k3 = 0;
    for (g = 0; g < t; g++) {
        k3++;
        k3%=2;
        if (k3 == 1)
            cout << k2 << ' ';
        else
            cout << k1 << ' ';
    }
    return 0;
}
