#include <bits/stdc++.h>

#define int long long

using namespace std;

const int MOD = 998244353;

int binpow(int x, int n)
{
    if (n == 0)
        return 1;
    
    if (n & 1)
        return (x * binpow(x, n - 1)) % MOD;
    int tmp = binpow(x, n / 2);
    return (tmp * tmp) % MOD;
}

int f(vector<int> &p)
{
    int ans = 0;
    for (int i = 0; i < p.size(); ++i)
        ans = (ans + binpow(p[i], i + 1)) % MOD;
    return ans;
}   

int32_t main()
{
    int n, q;
    cin >> n >> q;

    vector<int> p(n);
    for (int &x : p)
        cin >> x;
    
    vector<vector<int>> g(4);
    g[0] = {1, 2};
    g[1] = {0, 3};
    g[2] = {3, 0};
    g[3] = {2, 1};

    vector<int> ans(4);
    ans[0] = f(p);
    reverse(p.begin(), p.end());
    ans[1] = f(p);
    for (int i = 0; i < n; ++i)
        p[i] = n - p[i] + 1;
    ans[3] = f(p);
    reverse(p.begin(), p.end());
    ans[2] = f(p);

    int cur = 0;
    while (q-- > 0)
    {
        int b;
        cin >> b;

        if (b == 1)
            cur = g[cur][0];
        else
            cur = g[cur][1];
        cout << ans[cur] << ' ';

    }
}