#pragma GCC optimize("O3,unroll-loops")

#include <bits/stdc++.h>

using ll = long long int;
using ull = unsigned long long int;

using namespace std;

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

const int MOD = 998244353;

ll binpow(ll a, ll b)
{
    if (b == 0)
        return 1;
    if (b % 2 == 0)
    {
        ll x = binpow(a, b / 2);
        return x * x % MOD;
    }
    return binpow(a, b - 1) * a % MOD;
}

void solve()
{
    int n, q;
    cin >> n >> q;

    vector<int> p(n);
    for (int i = 0; i < n; i++)
        cin >> p[i];

    vector<int> b(q);
    for (int i = 0; i < q; i++)
        cin >> b[i];

    for (int i = 0; i < q; i++)
    {
        if (b[i] == 1)
        {
            reverse(all(p));
        }
        else if (b[i] == 2)
        {
            for (int i = 0; i < n; i++)
                p[i] = n - p[i] + 1;
        }
        else
        {
            vector<int> c(n);
            for (int i = 0; i < n; i++)
                c[p[i] - 1] = i + 1;
            p = c;
        }

        int ans = 0;
        for (int i = 0; i < n; i++)
        {
            ans += binpow(p[i], i + 1);
            ans %= MOD;
        }
        cout << ans << ' ';
    }
    cout << '\n';
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int t = 1;
#ifdef LOCAL
    freopen("C:/work/in.txt", "r", stdin);
    freopen("C:/work/out.txt", "w", stdout);
    cin >> t;
#endif

    while (t--)
        solve();
    return 0;
}