#include <bits/stdc++.h>
using namespace std;

#pragma GCC optimize("O3", "Ofast", "unroll-loops", "fast-math")
#pragma GCC target("avx", "avx2")

using ll = long long;
using lll = __int128;
using dd = long double;
using para = pair <ll, ll>;
using parad = pair <dd, dd>;
const ll mod = 998244353;
ll binp(ll a, ll n) {
    if (n == 1) return a;
    if (!(n & 1)) return binp(a * a % mod, n >> 1);
    return a * binp(a, n - 1) % mod;
}
ll f(const vector <ll>& p) {
    ll n = p.size() - 1;
    ll ans = 0;
    for (ll i = 1; i <= n; ++i) {
        ans += binp(p[i], i);
        if (ans >= mod) ans -= mod;
    }
    return ans;
}
vector <ll> upd(const vector <ll> p, ll type) {
    vector <ll> q(p.size());
    ll n = p.size() - 1;
    for (ll i = 1; i < p.size(); ++i) {
        if (type == 3) q[p[i]] = i;
        if (type == 2) q[i] = n - p[i] + 1;
        if (type == 1) q[i] = p[n - i + 1];
    }
    return q;
}
void solvetask() {
    ll n, qu;
    cin >> n >> qu;
    vector <ll> p(n + 1);
    for (ll i = 1; i <= n; ++i) {
        cin >> p[i];
    }
    set <ll> sb;
    vector <ll> b(qu);
    for (ll i = 0; i < qu; ++i) {
        cin >> b[i];
        sb.insert(b[i]);
    }
    vector <para> ss(qu, {-1, qu});
    map <ll, ll> dss;
    for (ll i = 0; i < qu; ++i) {
        ss[i].first = dss[b[i]] - 1;
        dss[b[i]] = i + 1;
    }
    dss.clear();
    for (ll i = qu - 1 ; i > -1; --i) {
        ss[i].second = dss[b[i]] + n;
        dss[b[i]] = i - n;
    }
    map <ll, vector <ll>> d;
    for (ll i = 1; i <= 3; ++i) {
        for (ll j = 0; j < n + 1; ++j) {
            d[i].push_back(0);
        }
    }
    vector <ll> ans(qu + 1);
    /*/if (sb.size() == 2 && sb.find(3) == sb.end()) {
        for (ll i = 0; i < qu; ++i) {
            if (b[i] == 1) {
            }
        }
    }/*/
    map <vector <ll>, ll> dv;
    dv[p] = 1;
    for (ll i = 1; i <= 3; ++i) {
        dv[upd(p, i)] = 1;
    }
    for (ll i = 1; i <= 3; ++i) {
        for (ll j = 1; j <= 3; ++j) {
            if (i != j) dv[upd(upd(p, i), j)] = 1;
        }
    }
    for (ll i = 1; i <= 3; ++i) {
        for (ll j = 1; j <= 3; ++j) {
            for (ll k = 1; k <= 3; ++k) {
                if (i != j && j != k && i != k) dv[upd(upd(upd(p, i), j), k)] = 1;
            }
        }
    }
    /*/for (auto& c : dv) {
        for (ll i = 1; i <= n; ++i) {
            cout << c.first[i] << ' ';
        }
        cout << "\n";
    }
    for (ll ij = 0; ij < qu; ++ij) {
        vector <ll> q1 = upd(p, b[ij]);
        if (dv[q1] != 1) {
            cout << "ALARM ";
        }
        cout << f(q1) << "\n";
        for (ll i = 1; i <= n; ++i) {
            cout << q1[i] << ' ';
            p[i] = q1[i];
        }
        cout << "\n";
    }/*/
    vector <vector <ll>> qt(4, vector <ll> (n + 1));
    for (ll i = 1; i <= n; ++i) {
        qt[1][i] = p[n - i + 1];
    }
    for (ll i = 1; i <= n; ++i) {
        qt[2][i] = n - p[i] + 1;
    }
    for (ll i = 1; i <= n; ++i) {
        qt[3][p[i]] = i;
    }
    ll qtf[4];
    for (ll i = 1; i <= 3; ++i) {
        qtf[i] = f(qt[i]);
    }
    ll pcost = f(p);
    if (sb.size() == 1) {
        ll cntc = 1;
        for (ll i = 0; i < qu; ++i) {
            if (cntc == 0) {
                cout << pcost << ' ';
                cntc = 1;
            } else {
                cout << qtf[b[i]] << ' ';
                cntc = 0;
            }
        }
        cout << "\n";
        return;
    }
    vector <ll> q(n + 1);
    for (ll ij = 0; ij < qu; ++ij) {
        if (b[ij] == 1) {
            for (ll i = 1; i <= n; ++i) {
                q[i] = p[n - i + 1];
            }
            for (ll i = 1; i <= n; ++i) {
                p[i] = q[i];
            }
            ll ans1 = 0;
            for (ll i = 1; i <= n; ++i) {
                ans1 += binp(p[i], i);
                if (ans1 >= mod) ans1 -= mod;
            }
            cout << ans1 << " ";
        } else if (b[ij] == 2) {
            for (ll i = 1; i <= n; ++i) {
                q[i] = n - p[i] + 1;
            }
            for (ll i = 1; i <= n; ++i) {
                p[i] = q[i];
            }
            ll ans1 = 0;
            for (ll i = 1; i <= n; ++i) {
                ans1 += binp(p[i], i);
                if (ans1 >= mod) ans1 -= mod;
            }
            cout << ans1 << " ";
        } else {
            for (ll i = 1; i <= n; ++i) {
                q[p[i]] = i;
            }
            for (ll i = 1; i <= n; ++i) {
                p[i] = q[i];
            }
            ll ans1 = 0;
            for (ll i = 1; i <= n; ++i) {
                ans1 += binp(p[i], i);
                if (ans1 >= mod) ans1 -= mod;
            }
            cout << ans1 << " ";
        }
        if (dv[p] != 1) {
            while (true) {}
        }
    }
}
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll tests = 1;
    //cin >> tests;
    while (tests--) {
        solvetask();
    }
    return 0;
}