#ifdef LOCAL
#define _CRT_SECURE_NO_WARNINGS
#endif

#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <cmath>
#include <string>
#include <ctime>
#include <climits>
#include <iomanip>
#include <numeric>
#include <random>
#include <array>
#include <bitset>
#include <unordered_map>
#include <unordered_set>

#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")


using ll = long long;
using ld = long double;
using namespace std;

template<typename T1, typename T2>
istream& operator>>(istream& in, pair<T1, T2>& p) {
	in >> p.first >> p.second;
	return in;
}

template<typename T1, typename T2>
ostream& operator<<(ostream& out, pair<T1, T2>& p) {
	out << p.first << " " << p.second;
	return out;
}

template<typename T>
istream& operator>>(istream& in, vector<T>& a) {
	for (auto& el : a) {
		in >> el;
	}
	return in;
}

template<typename T>
ostream& operator<<(ostream& out, vector<T>& a) {
	if (a.empty()) return out;
	out << a[0];
	for (int i = 1; i < (int)a.size(); i++) {
		out << " " << a[i];
	}
	return out;
}

const int mod = 998244353;
const int C = 4 * 4 * 4;

int add(int x, int y) {
	if (x + y >= mod) return x + y - mod;
	return x + y;
}

int sub(int x, int y) {
	if (x < y) return x - y + mod;
	return x - y;
}

int mul(int x, int y) {
	return (1ll * x * y) % mod;
}

int bp(int x, int p) {
	if (p == 0) return 1;
	if (p == 1) return x;
	if (p % 2 == 0) {
		return bp(mul(x, x), p / 2);
	} else {
		return mul(x, bp(x, p - 1)) % mod;
	}
}

int get_val(vector<int>& p) {
	int n = (int)p.size();
	int ans = 0;
	for (int i = 0; i < n; i++) {
		ans = add(ans, bp(p[i], i + 1));
	}
	return ans;
}

vector<int> op1(vector<int>& p) {
	vector<int> q = p;
	reverse(q.begin(), q.end());
	return q;
}

vector<int> op2(vector<int>& p) {
	int n = (int)p.size();
	vector<int> q(n);
	for (int i = 0; i < n; i++) {
		q[i] = n - p[i] + 1;
	}
	return q;
}

vector<int> op3(vector<int>& p) {
	int n = (int)p.size();
	vector<int> q(n);
	for (int i = 0; i < n; i++) {
		q[p[i] - 1] = i + 1;
	}
	return q;
}

void solve() {
	
	int n, q;
	cin >> n >> q;
	vector<int> p(n);
	cin >> p;
	vector<vector<int>> pers(C);
	vector<vector<int>> to(C, vector<int>(4));
	vector<int> vals(C);
	for (int mask = 0; mask < C; mask++) {
		vector<int> p1 = p;
		int cur = mask;
		for (int i = 0; i < 3; i++) {
			int bit = cur % 4;
			cur /= 4;
			if (bit == 0) {
				p1 = op1(p1);
			} else if (bit == 1) {
				p1 = op2(p1);
			} else if(bit == 2) {
				p1 = op3(p1);
			} else {
				p1 = p1;
			}
		}
		vals[mask] = get_val(p1);
		pers[mask] = p1;
	}
	for (int mask = 0; mask < C; mask++) {
		vector<int> p1 = pers[mask];
		for (int op = 1; op <= 3; op++) {
			vector<int> p2 = p1;
			if (op == 1) {
				p2 = op1(p2);
			} else if (op == 2) {
				p2 = op2(p2);
			} else {
				p2 = op3(p2);
			}
			for (int mask1 = 0; mask1 < C; mask1++) {
				if (pers[mask1] == p2) {
					to[mask][op] = mask1;
					break;
				}
			}
		}
	}
	int cur = C - 1;
	for (int i = 0; i < q; i++) {
		int t;
		cin >> t;
		cur = to[cur][t];
		cout << vals[cur] << " ";
	}
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
#ifdef LOCAL
	freopen("input.txt", "r", stdin);
	ld start_time = clock();
#endif
	solve();
#ifdef LOCAL
	ld finish_time = clock();
	cout << "\n" << "finished in: " << (finish_time - start_time) / CLOCKS_PER_SEC;
#endif
	return 0;
}