#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <cstdio>
#include <cstdint>
#include <set>
#include <map>
#include <deque>
#include <queue>
#include <unordered_set>
#include <unordered_map>
#include <chrono>
#include <ctime>
#include <random>
using namespace std;
#define int long long
#define all(x) (x).begin(), (x).end()
#define rep(n) for (int rep_i = 0; rep_i < n; rep_i++)
#define cin(v) for (auto& e : v) cin >> e;
#define cout(v) for (auto e : v) cout << e << " "; cout << endl;
using ll = long long;
using ull = unsigned long long;
using db = double;
using i32 = int32_t;
using vi = vector <int>;
using vll = vector <ll>;
using pii = pair <int, int>;
using pll = pair <ll, ll>;
#define sq(x) ((x) * (x))
mt19937 rng(time(0));
int rand(int l, int r) {
	return (rng() % (r - l + 1)) + l;
}
void fastw() {
	ios_base::sync_with_stdio(0); cin.tie(0);
}

void solve();

signed main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
#endif
	fastw();
	solve();
}

const int md = 998244353;

int binpow(int a, int b) {
	int res = 1;
	while (b) {
		if (b & 1) {
			res = (res * a) % md;
		}
		a = (a * a) % md;
		b >>= 1;
	}

	return res;
}

void solve() {

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

	if (n <= 1000 && q <= 1000) {
		for (auto qr : b) {
			int r = 0;
			if (qr == 1) {
				reverse(all(p));
			}
			else if (qr == 2) {
				for (int i = 0; i < n; i++) {
					p[i] = n - p[i] + 1;
				}
			}
			else {
				vector <int> nw(n);
				for (int i = 0; i < n; i++) {
					nw[p[i] - 1] = i + 1;
				}
				p = nw;
			}
			for (int i = 0; i < n; i++) {
				r += binpow(p[i], i + 1);
				r %= md;
			}
			cout << r << " ";
		}
	}
	cout << endl;
}