#include <bits/stdc++.h>

using namespace std;

vector<int> f(vector<int> p) {
    int n = p.size();
    vector<int> q(n);
    for (int i = 0; i < n; ++i) {
        q[i] = p[n - i - 1];
    }
    return q;
}

vector<int> s(vector<int> p) {
    int n = p.size();
    vector<int> q(n);
    for (int i = 0; i < n; ++i) {
        q[i] = n - p[i] + 1;
    }
    return q;
}

vector<int> t(vector<int> p) {
    int n = p.size();
    vector<int> q(n);
    for (int i = 0; i < n; ++i) {
        q[p[i] - 1] = i + 1;
    }
    return q;
}

vector<int> query(vector<int> p, int type) {
    if (type == 1) return f(p);
    else if (type == 2) return s(p);
    else return t(p);
}

const int mod = 998244353;

int add(int a, int b) {
    return a + b >= mod ? a + b - mod : a + b;
}
int mult(int a, int b) {
    return a * 1ll * b % mod;
}
int bpow(int a, int b) {
    int ans = 1;
    while (b) {
        if (b & 1) ans = mult(ans, a);
        a = mult(a, a);
        b >>= 1;
    }
    return ans;
}

int calc(vector<int> p) {
    int n = p.size();

    int ans = 0;
    for (int i = 0; i < n; ++i) {
        ans = add(ans, bpow(p[i], i + 1));
    }

    return ans;
}

void print(vector<int> p) {
    int n = p.size();
    for (int i = 0; i < n; ++i) {
        cout << p[i] << " \n"[i == n - 1];
    }
}

int ans[10];

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(nullptr);

    int n, t;
    cin >> n >> t;

    vector<int> p(n);
    for (int i = 0; i < n; ++i) {
        cin >> p[i];
    }
    
    int ptr = 1;
    map<vector<int>, int> st;
    queue<vector<int>> q;
    q.push(p), st[p] = ptr++;

    while (!q.empty()) {
        vector<int> cur = q.back();
        q.pop();

        for (int i = 1; i <= 3; ++i) {
            vector<int> next = query(cur, i);
            if (!st.count(next)) {
                st[next] = ptr++;
                q.push(next);
            }
        }
    }

    map<pair<int, int>, int> go;
    for (auto& [cur, v]: st) {
        ans[v] = calc(cur);
        for (int i = 1; i <= 3; ++i) {
            vector<int> next = query(cur, i);
            int u = st[next];
            
            go[{v, i}] = u;
        }
    }

    int cur = 1;
    for (int i = 0; i < t; ++i) {
        int next; cin >> next;
        cur = go[{cur, next}];
        cout << ans[cur] << " \n"[i == t - 1];  
    }

    return 0;
}