//#pragma GCC optimize("Ofast,unroll-loops")
//#pragma GCC target("avx2")
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <numeric>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
using namespace std;
using ll = long long;
using ld = long double;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvll = vector<vll>;
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()

void in(vll& a) {
	for (ll& i : a) cin >> i;
}

void out(vll& a) {
	for (ll& i : a) cout << i << ' ';
	cout << '\n';
}

void in(vpll& a) {
	for (pll& i : a) cin >> i.first >> i.second;
}

void in(vvll& a) {
	for (auto& v : a) {
		for (auto& x : v) cin >> x;
	}
}

const ll MOD = 998244353;

ll bp(ll a, ll n) {
	ll r = 1;
	while (n) {
		if (n & 1) {
			r *= a;
			r %= MOD;
		}
		a *= a;
		a %= MOD;
		n >>= 1;
	}
	return r;
}

void solve() {
	ll n, q; cin >> n >> q;
	vll p(n), qs(q);
	in(p);
	in(qs);
	for (auto t : qs) {
		if (t == 1) {
			reverse(all(p));
		} else if (t == 2) {
			for (ll i = 0; i < n; ++i) {
				p[i] = n - p[i] + 1;
			}
		} else {
			vll p2 = p;
			for (ll i = 0; i < n; ++i) {
				p2[p[i] - 1] = i + 1;
			}
			p = p2;
		}
		ll c = 0;
		for (ll i = 0; i < n; ++i) {
			c += bp(p[i], i + 1);
			c %= MOD;
		}
		cout << c << ' ';
	}
}

int main() {
	ios::sync_with_stdio(0); cin.tie(0);
	ll ts = 1;
	//cin >> ts;
	while (ts--) solve();
}