
#include <bits/stdc++.h>

#define int long long
#define ll long long
#define pii pair<int, int>

using namespace std;

const int mod = 998244353;

int bin_pow(int x, int k) {
    if (k == 0) {
        return 1;
    }
    if (k % 2 == 0) {
        int bp = bin_pow(x, k / 2);
        return (bp * bp) % mod;
    }
    return (bin_pow(x, k - 1) * x) % mod;
}

signed main() {
    int n, q; cin >> n >> q;
    vector<int> p(n), b(q);
    for (auto &x : p)
        cin >> x;
    for (auto &x : b)
        cin >> x;
    /*
    for (int i = 0; i < q; ++i) {
        if (b[i] == 1) {
            for (int j = 0; j < n / 2; ++j) {
                swap(p[j], p[n - j - 1]);
            }
        } else if (b[i] == 2) {
            for (int j = 0; j < n; ++j) {
                p[j] = n + 1 - p[j];
            }
        } else {
            vector<int> qs(n);
            for (int j = 0; j < n; ++j) {
                qs[p[j] - 1] = j + 1;
            }
            p = qs;
        }
        int ans = 0;
        for (int j = 0; j < n; ++j) {
            ans += bin_pow(p[j], j + 1);
            ans %= mod;
        }
        cout << ans << ' ';
    }*/
    int a = 0;
    for (int j = 0; j < n; ++j) {
        a += bin_pow(p[j], j + 1);
        a %= mod;
    }
    if (b[0] == 1) {
        for (int j = 0; j < n / 2; ++j) {
            swap(p[j], p[n - j - 1]);
        }
    } else if (b[0] == 2) {
        for (int j = 0; j < n; ++j) {
            p[j] = n + 1 - p[j];
        }
    } else {
        vector<int> qs(n);
        for (int j = 0; j < n; ++j) {
            qs[p[j] - 1] = j + 1;
        }
        p = qs;
    }
    int a1 = 0;
    for (int j = 0; j < n; ++j) {
        a1 += bin_pow(p[j], j + 1);
        a1 %= mod;
    }
    for (int i = 0; i < q; ++i) {
        if (i % 2 == 0) {
            cout << a1 << ' ';
        } else {
            cout << a << ' ';
        }
    }
}

