#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <iomanip>
#include <deque>
#include <queue>
#include <numeric>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <random>
using namespace std;

#define ll long long
#define ld long double
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
mt19937 rnd(time(0));

const int MOD = 998244353;


int bin_pow(int x, int y) {
	if (y == 0) return 1;
	if (y % 2 == 0) {
		int b = bin_pow(x, y / 2);
		return 1ll * b * b % MOD;
	}
	else {
		return 1ll * x * bin_pow(x, y - 1) % MOD;
	}
}

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

int n;
vector<int> p;
vector<int> q;
int ans;
void type_1() {
	for (int i = 0; i < n; i++) {
		q[i] = p[n - i - 1];
	}
	swap(q, p);
}
void type_2() {
	for (int i = 0; i < n; i++) {
		q[i] = n - p[i] - 1;
	}
	swap(q, p);
}
void type_3() {
	for (int i = 0; i < n; i++) {
		q[p[i]] = i;
	}
	swap(q, p);
}

void get(int t) {
	if (t == 1) type_1();
	if (t == 2) type_2();
	if (t == 3) type_3();
}

void get_ans() {
	ans = 0;
	for (int i = 0; i < n; i++) {
		ans = add(ans, bin_pow(p[i] + 1, i + 1));
	}
}
void solve() {
	int m;
	cin >> n >> m;
	p.assign(n, 0);
	for (int i = 0; i < n; i++) {
		cin >> p[i];
		p[i]--;
	}
	q.assign(n, 0);
	int pref = -1;
	int cnt = 0;
	int p_ans = 0;
	do {
		int v;
		cin >> v;
		if (v != pref) {
			if (cnt % 2 == 1 and cnt > 1) {
				get(pref);
				get_ans();
			}
			cnt = 1;
			get(v);
			get_ans();
			p_ans = ans;
		}
		else if (cnt == 1) {
			get(v);
			get_ans();
			cnt++;
		}
		else {
			cnt++;
		}
		pref = v;
		m--;
		if (cnt % 2 == 1) {
			cout << p_ans << " ";
		}
		else {
			cout << ans << " ";
		}
	} while (m > 0);
	
}

signed main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	solve();
}