#include <bits/stdc++.h>
#pragma optimize("Ofast")
#pragma optimize("O3")

using namespace std;

using ll = long long;

const int INF = 1e9 + 7, mod = 998244353;

#ifdef LOCAL
const int MAXN = 1e2;
#else
const int MAXN = 1e6;
#endif

int n;
vector<int> p, op;

ll bp(ll a, ll b) {
    if (b == 0)
        return 1;
    if (b % 2)
        return (a * bp(a, b - 1)) % mod;
    ll res = bp(a, b / 2);
    return (res * res) % mod;
}

void a1() {
    reverse(p.begin(), p.end());
}

void a2() {
    vector<int> now(n);
    for (int i = 0; i < n; i++) {
        now[i] = n - p[i] - 1;
    }
    p = now;
}

void a3() {
    vector<int> now(n);
    for (int i = 0; i < n; i++)
        now[p[i]] = i;
    p = now;
}

void print() {
    for (int i : p)
        cout << i << ' ';
    cout << '\n';
}

ll clc() {
    ll res = 0;
    for (int i = 0; i < n; i++) {
        res += bp(p[i] + 1, i + 1);
        res %= mod;
    }
    return res;
}

map<int, int> mp;

int c(vector<int> a){
    int now = 0;
    for(int i : a){
        now *= 4;
        now += i;
    }
    return now;
}

void get(vector<int> a) {
    if (mp[c(a)] != 0)
        return;
    for (int i : a)
        if (i == 1)
            a1();
        else if (i == 2)
            a2();
        else
            a3();
    int res = clc();
    mp[c(a)] = res;
    // for(int i : a) cout << i << ' ';
    // cout << '\n';
    reverse(a.begin(), a.end());
    for (int i : a)
        if (i == 1)
            a1();
        else if (i == 2)
            a2();
        else
            a3();
}

vector<int> now;

vector<int> norm(vector<int> a) {
    int n = a.size();
    vector<int> rem(n);
    int ind = -1;
    for (int i = 0; i < n; i++) {
        if (a[i] == 3 && ind != -1) {
            rem[i] = rem[ind] = 1;
            ind = -1;
        } else if(a[i] == 3){
            ind = i;
        }
    }
    vector<int> b;
    int aind = -1, bind = -1;
    for(int i = 0; i < n; i++){
        if(rem[i]) continue;
        if(a[i] == 1 && aind != -1) {
            rem[i] = rem[aind] = 1;
            aind = -1;
        }
        else if(a[i] == 1)
            aind = i;
        else if(a[i] == 2 && bind != -1) {
            rem[i] = rem[bind] = 1;
            bind = -1;
        }
        else if(a[i] == 2)
            bind = i;
        else if(a[i] == 3){
            aind = -1;
            bind = -1;
        }
    }
    vector<int> ans;
    for(int i = 0; i < n; i++)
        if(rem[i] == 0)
            ans.push_back(a[i]);
    // for(int i : a) cout << i << ' ';
    // cout << '\n';
    // for(int i : ans) cout << i << ' ';
    // cout << '\n';
    return ans;
}

void dfs(int len) {
    get(norm(now));
    if (len == 7) {
        return;
    }
    now.push_back(1);
    dfs(len + 1);
    now.pop_back();
    now.push_back(2);
    dfs(len + 1);
    now.pop_back();
    now.push_back(3);
    dfs(len + 1);
    now.pop_back();
}

int main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int q;
    cin >> n >> q;
    p.resize(n);
    for (int &i : p) {
        cin >> i;
        i--;
    }
    dfs(0);
    int type;
    while (cin >> type) {
        op.push_back(type);
        op = norm(op);
        cout << mp[c(op)] << ' ';
    }
    return 0;
}
