#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define int long long
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define F first
#define S second
#define Vector2 pair <int, int>
#define print(a) for (const auto& x : a) cout << x << ' ';

const int MOD = 998244353;

int sum(int a, int b) {
	a += b;
	if (a >= MOD) {
		a -= MOD;
	}
	return a;
}

int binpow(int x, int pw) {
	int res = 1;
	while (pw) {
		if (pw & 1) {
			(res *= x) %= MOD;
		}
		(x *= x) %= MOD;
		pw >>= 1;
	}
	return res;
}

void solve()
{
	int n, q;
	cin >> n >> q;
	vector <int> a(n);
	for (auto& x : a) {
		cin >> x;
	}
	int ans = 0, ans2 = 0, ans3 = 0, ans4 = 0;
	auto get = [&] () -> int {
		int res = 0;
		for (int i = 0; i < n; ++i) {
			res = sum(res, binpow(a[i], i + 1));
		}
		return res;
	};
	ans = get();
	vector <int> b = a;
	for (int i = 0; i < n; ++i) {
		b[i] = a[n - i - 1];
	}
	a = b;
	ans2 = get();
	for (int i = 0; i < n; ++i) {
		b[i] = a[n - i - 1];
	}
	for (int i = 0; i < n; ++i) {
		b[i] = n - a[i] + 1;
	}
	a = b;
	ans3 = get();
	for (int i = 0; i < n; ++i) {
		b[i] = n - a[i] + 1;
	}
	for (int i = 0; i < n; ++i) {
		b[a[i] - 1] = i + 1;
	}
	a = b;
	ans4 = get();
	bool flag = true;
	while (q--) {
		int t;
		cin >> t;
		int sum = 0;
		if (t == 1) {
			cout << (flag ? ans2 : ans) << ' ';
			flag ^= 1;
		} else if (t == 2) {
			cout << (flag ? ans3 : ans) << ' ';
			flag ^= 1;
		} else {
			cout << (flag ? ans4 : ans) << ' ';
			flag ^= 1;
		}
	}
}

signed main()
{
	cin.tie(nullptr)->ios_base::sync_with_stdio(false);
	solve();
	return 0;
}