#include <iostream>
#include <vector>
using namespace std;

long long u = 998244353;

long long power(long long p, long long i)
{
    if (i == 1) return p;
    long long x = power(p, i / 2);
    x = (x * x) % u;
    if (i % 2 == 1) x = (x * p) % u;
    return x;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, q; cin >> n >> q;
    vector<vector<long long>> A(n, vector<long long>(8));
    vector<vector<long long>> change(8, vector<long long>(3));
    change = {
        {1, 2, 3},
        {0, 4, 5},
        {4, 0, 6},
        {6, 5, 0},
        {2, 1, 7},
        {7, 3, 1},
        {3, 7, 2},
        {5, 6, 4},
    };
    for (int i = 1; i <= n; ++i){
        long long p; cin >> p;
        int j = i - 1;
        A[j][0] = power(p, i);
        A[j][1] = power(p, n - i + 1);
        A[j][2] = power(n - p + 1, i);
        A[j][3] = power(i, p);
        A[j][4] = power(n - p + 1, n - i + 1);
        A[j][5] = power(n - i + 1, p);
        A[j][6] = power(i, n - p + 1);
        A[j][7] = power(n - i + 1, n - p + 1);
    }
    vector<long long> ans(8);
    for (int i = 0; i < n; ++i){
        for (int j = 0; j < 8; ++j){
            ans[j] = (ans[j] + A[i][j]) % u;
        }
    }
    int cur_state = 0;
    for (int i = 0; i < q; ++i){
        int ch; cin >> ch;
        --ch;
        cur_state = change[cur_state][ch];
        cout << ans[cur_state] << ' ';
    }
}
