#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ull = unsigned long long;
using db = double;
using ldb = long double;

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

const int mod = 998244353;

struct hash_t{
	int v1;
	ull v2;

	hash_t() {
		v1 = v2 = 0;
	}

	hash_t(int _v1, ull _v2) {
		v1 = _v1;
		v2 = _v2;
	}

	hash_t operator+(const hash_t& h) const {
		hash_t res = hash_t(v1 + h.v1, v2 + h.v2);
		if (res.v1 >= mod) res.v1 -= mod;
		return res;
	}

	hash_t operator-(const hash_t& h) const {
		hash_t res = hash_t(v1 - h.v1, v2 - h.v2);
		if (res.v1 < 0) res.v1 += mod;
		return res;
	}

	hash_t operator*(const hash_t& h) const {
		return hash_t(1ll * v1 * h.v1 % mod, v2 * h.v2);
	}
};

ll bin_pow(int x, int pow) {
	if (pow == 0) return 1;
	if ((pow & 1) == 0) {
		ll res = bin_pow(x, pow / 2);
		return res * res % mod;
	} else {
		return bin_pow(x, pow - 1) * x % mod;
	}
}


vector<set<int>> g;

void euler(int v) {
	while (!g[v].empty()) {
		int to = *g[v].begin();
		g[v].erase(g[v].begin());
		g[to].erase(v);
		euler(to);
	}
	cout << v << ' ';
}


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

	int n, q;
	cin >> n >> q;
	vector<int> p(n);

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

	for (int i = 0; i < q; i++) {
		int b; cin >> b;
		vector<int> p1(n);
		
		ll ans = 0;
		for (int i = 0; i < n; i++) {
			if (b == 1) p1[i] = p[n - i - 1];
			if (b == 2) p1[i] = n - p[i] + 1;
			if (b == 3) p1[p[i] - 1] = i + 1;

			ans += bin_pow(p1[i], (i + 1));
			ans %= mod;
		}
		p = p1;
		cout << ans << ' ';
	}

	return 0;
}
