#include <bits/stdc++.h>

#ifndef LOCAL
#pragma GCC optimize("O3")
#endif

#define all(x) (x).begin(), (x).end()

using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
using pii = pair<int, int>;

const int mod = 998244353;

struct mint
{
    int x;

    mint(int x = 0) : x(x) {}

    friend mint operator+(mint a, mint b)
    {
        int res = a.x + b.x;
        return res - (res >= mod ? mod : 0);
    }

    friend mint operator-(mint a, mint b)
    {
        int res = a.x - b.x;
        return res + (res < 0 ? mod : 0);
    }

    friend mint operator*(mint a, mint b)
    {
        return ll(a.x) * b.x % mod;
    }
};

mint mpow(mint a, ll b)
{
    mint res = 1;
    while (b)
    {
        if (b & 1)
        {
            res = res * a;
        }
        a = a * a;
        b >>= 1;
    }
    return res;
}

void solve()
{
    int n, q;
    cin >> n >> q;
    vector<int> p(n);
    for (int i = 0; i < n; ++i)
    {
        cin >> p[i];
        --p[i];
    }
    vector<mint> ans(8);
    for (int i = 0; i < 8; ++i)
    {
        vector<int> e = p;
        if (i >> 2 & 1)
        {
            for (int i = 0; i < n; ++i)
            {
                e[p[i]] = i;
            }
        }
        if (i >> 1 & 1)
        {
            for (int i = 0; i < n; ++i)
            {
                e[i] = n - e[i] - 1;
            }
        }
        if (i & 1)
        {
            reverse(all(e));
        }
        for (int j = 0; j < n; ++j)
        {
            ans[i] = ans[i] + mpow(e[j] + 1, j + 1);
        }
    }
    vector<int> g1 = {1, 0, 3, 2, 5, 4, 7, 6};
    vector<int> g2 = {2, 3, 0, 1, 6, 7, 4, 5};
    vector<int> g3 = {4, 6, 5, 7, 0, 2, 1, 3};
    int cur = 0;
    for (int i = 0; i < q; ++i)
    {
        int b;
        cin >> b;
        if (b == 1)
        {
            cur = g1[cur];
        }
        else if (b == 2)
        {
            cur = g2[cur];
        }
        else
        {
            cur = g3[cur];
        }
        cout << ans[cur].x << ' ';
    }
    cout << '\n';
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
#endif
    solve();
}