#include <iostream>
#include <algorithm>
#include <iomanip>
#include <cmath>
#include <numeric>
#include <cassert>
#include <vector>
#include <map>

#define all(a) a.begin(), a.end()
#define int int64_t

using namespace std;

const int MOD = 998244353;

int pw(int a, int n) {
	if (n == 0) return 1;
	if (n % 2 == 0) {
		int nw = pw(a, n / 2);
		return nw * nw % MOD;
	} else {
		return a * pw(a, n - 1) % MOD;
	}
}

map<vector<int>, int> mp;

vector<vector<int>> gr;
vector<int> ans;

int n;

int get_ans(vector<int> a) {
	int ans = 0;
	for (int i = 1; i <= n; ++i) {
		ans += pw(a[i], i);
		ans %= MOD;
	}
	return ans;
}

vector<int> q1(vector<int> a) {
	vector<int> b(n + 1);
	for (int i = 1; i <= n; ++i) {
		b[i] = a[n - i + 1];
	}
	return b;
}

vector<int> q2(vector<int> a) {
	vector<int> b(n + 1);
	for (int i = 1; i <= n; ++i) {
		b[i] = n - a[i] + 1;
	}
	return b;
}

vector<int> q3(vector<int> a) {
	vector<int> b(n + 1);
	for (int i = 1; i <= n; ++i) {
		b[a[i]] = i;
	}
	return b;
}

int now = 2;

void dfs(vector<int> a) {
	vector<int> u;
	u = q1(a);
	if (mp[u] == 0) {
		mp[u] = now++;
		ans[mp[u]] = get_ans(u);
		dfs(u);
	}
	gr[mp[a]].push_back(mp[u]);
	u = q2(a);
	if (mp[u] == 0) {
		mp[u] = now++;
		ans[mp[u]] = get_ans(u);
		dfs(u);
	}
	gr[mp[a]].push_back(mp[u]);
	u = q3(a);
	if (mp[u] == 0) {
		mp[u] = now++;
		ans[mp[u]] = get_ans(u);
		dfs(u);
	}
	gr[mp[a]].push_back(mp[u]);
}

signed main() {
	int q;
	cin >> n >> q;
	vector<int> a(n + 1);
	for (int i = 1; i <= n; ++i) cin >> a[i];
	ans.resize(100);
	gr.resize(100);
	ans[1] = get_ans(a);
	mp[a] = 1;
	dfs(a);
	int v = 1;
	while (q--) {
		int b;
		cin >> b;
		v = gr[v][b - 1];
		cout << ans[v] << " ";
	}
}