#pragma GCC optimize("O3,unroll-loops")
#include<vector>
#pragma GCC target("avx,avx2")
#include<bits/stdc++.h>
using namespace std;

using ll = long long;
using ld = long double;

#ifdef DBG
#define $(x) x
#else
#define $(x)
#endif

#define debug(x) $(cout << #x << " = " << x << endl)

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

constexpr int INF = 1e9 + 7;
constexpr ll LINF = 1e18 + 7;
constexpr ll MOD = 998244353;

void WA() {
    cout << "THIS IS WRONG ANSWER" << endl;
    exit(0);
}

void RE() {
    exit(1);
}

void TL() {
    unsigned x = 0;
    while (true) x++;
}

ll bpow(ll a, ll p) {
    if (p == 0) return 1;
    if (p & 1) return a * bpow(a, p - 1) % MOD;
    a = bpow(a, p / 2);
    return a * a % MOD;
}

void solve() {
    int n, q;
    cin >> n >> q;
    vector<int> a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        a[i]--;
    }

    vector<ll> c(8, 0);

    for (int rt = 0; rt <= 1; ++rt) {
        for (int h = 0; h <= 1; ++h) {
            for (int v = 0; v <= 1; ++v) {
                vector<int> b = a;
                if (rt) {
                    vector<int> nb(n);
                    for (int i = 0; i < n; ++i) {
                        nb[b[i]] = i;
                    }
                    swap(b, nb);
                }
                if (h) {
                    reverse(all(b));
                }

                if (v) {
                    for (int i = 0; i < n; ++i) {
                        b[i] = n - b[i] - 1;
                    }
                }

                ll ct = 0;
                for (int i = 0; i < n; ++i) {
                    ct += bpow(b[i] + 1, i + 1);
                    if (ct >= MOD) ct -= MOD;
                }

                c[rt * 4 + h * 2 + v] = ct;
            }
        }
    }
 
    bool rt = 0;
    bool h = 0, v = 0;

    for (int i = 0; i < q; ++i) {
        int x;
        cin >> x;
        if (x == 1) {
            h ^= 1;            
        } else if (x == 2) {
            v ^= 1;
        } else {
            rt ^= 1;
            swap(v, h);
        }

        ll nm = (int)rt * 4 + (int)h * 2 + (int)v;

        cout << c[nm] << '\n';
    }
}

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

    int t = 1;
    $(cin >> t);
    while (t--) {
        debug("=-=-=-=-=-=-=-=");
        solve();
    }
}