#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define ld long double
#define pb push_back
#define f first
#define s second
#define all(x) x.begin(), x.end()
#define ub upper_bound
#define lb lower_bound
#define mp make_pair
#define pll pair<ll, ll>

mt19937_64 rnd(chrono::steady_clock::now().time_since_epoch().count());

const ll mod = 998244353;

ll b_pow(ll n, ll st) {
    ll ans = 1;
    while (st > 0) {
        if (st % 2) {
            ans *= n;
            ans%=mod;
            st--;
        }
        else {
            st/=2;
            n*=n;
            n%=mod;
        }
    }
    return ans;
}

ll sum = 0;
ll q[100100];

ll n;

vector<ll> ch1(vector<ll> p) {
    vector<ll> b = p;
    reverse(all(b));
    return b;
}

vector<ll> ch2(vector<ll> p) {
    vector<ll> b = p;
    for (int i = 0; i < n; i++) {
        b[i] = n - b[i] + 1;
    }
    return b;
}

vector<ll> ch3(vector<ll> p) {
    vector<ll> b(n);
    for (int i = 0; i < n; i++) {
        b[p[i] - 1] = i + 1;
    }
    return b;
}

map<pair<ll, int>, pll> al;
map<ll, vector<pll>> g;

ll to_hash(vector<ll> p) {
    ll ans = 0;
    ll p1 = 127;
    ll md = 1e9 + 9;
    for (int i = 0; i < p.size(); i++) {
        ans *= p1;
        ans %= md;
        ans += p[i];
        ans %= md;
    }
    return ans;
}

map<ll, bool> used;

vector<ll> dfs(ll nw, ll need, vector<ll> b) {
    used[nw] = 1;
    if (nw == need)
        return b;
    for (auto [op, hs] : g[nw]) {
        if (used[hs])
            continue;
        if (op == 1)
            b = ch1(b);
        if (op == 2)
            b = ch2(b);
        if (op == 3)
            b = ch3(b);
        vector<ll> an = dfs(hs, need, b);
        if (an[0] != -1)
            return an;
    }
    return {-1};
}

vector<ll> p;

void solve() {
    ll m;
    cin >> n >> m;
    p.resize(n);
    vector<ll> op(3, 0);
    for (int i = 0; i < n; i++) {
        cin >> p[i];
    }
    ll hs = 0;
    for (int i = 0; i < m; i++) {
        cin >> q[i];
        ll ans = 0;
        if (al.find(mp(hs, q[i])) == al.end()) {
            vector<ll> b = dfs(0, hs, p);
            if (q[i] == 1)
                b = ch1(b);
            if (q[i] == 2)
                b = ch2(b);
            if (q[i] == 3)
                b = ch3(b);
            ans = 0;
            for (int i = 0; i < b.size(); i++) {
                ans += b_pow(b[i], i + 1);
                ans %= mod;
            }
            al[mp(hs, q[i])] = {to_hash(b), ans};
            g[hs].pb({q[i], to_hash(b)});
            used.clear();
        }
        cout << al[mp(hs, q[i])].s << ' ';
        hs = al[mp(hs, q[i])].f;
    }
   // assert(al.size() <= 8);
}

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    ll t = 1;
    //cin >> t;
    while (t--) {
        solve();
        cout << '\n';
    }
    return 0;
}
