#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <cmath>
#include <algorithm>
#include <bitset>
#include <random>
using namespace std;

#define int long long

const int Mod = 998'244'353;
const int Mx = 1e6 + 10;
int P[Mx], Qs[Mx];
int N, Q;

int to_P(int x, int p) {
    //cout << p << endl;
    if (p == 0) {
        return 1;
    }
    if (p == 1) {
        return x;
    }
    return ((__int128)to_P(x, p / 2) * to_P(x, p / 2) * to_P(x, p % 2)) % Mod;
}

signed main() {
    cin >> N >> Q;
    for (int i = 1; i <= N; i++) {
        cin >> P[i];
    }
    for (int I = 0; I < Q; I++) {
        int par;
        cin >> par;
        for (int i = 1; i <= N; i++) {
            if (par == 1) {
                Qs[i] = P[N - i + 1];
            }
            else if (par == 2) {
                Qs[i] = N - P[i] + 1;
            }
            else {
                Qs[P[i]] = i;
            }
        }
        int s = 0;
        for (int i = 1; i <= N; i++) {
            P[i] = Qs[i];
            s += to_P(P[i], i);
            s %= Mod;
        }
        cout << s << '\n';
    }
}

