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

template<typename T>
istream& operator>>(istream& stream, vector<T>& a) {
    for (auto& x : a) stream >> x;
    return stream;
}
template<typename T>
ostream& operator<<(ostream& stream, vector<T>& a) {
    for (auto& x : a) stream << x << ' ';
    cout << '\n';
    return stream;
}
template<typename T1, typename T2>
istream& operator>>(istream& stream, vector<pair<T1, T2>>& a) {
    for (auto& [x, y] : a) stream >> x >>  y;
    return stream;
}
template<typename T1, typename T2>
ostream& operator<<(ostream& stream, vector<pair<T1, T2>>& a) {
    for (auto& [x, y] : a) stream << x << ' ' << y << '\n';
    return stream;
}
template<typename T1, typename T2>
inline void amax(T1& a, T2 b) {
    a = max(a, b);
}
template<typename T1, typename T2>
inline void amin(T1& a, T2 b) {
    a = min(a, b);
}

#define int int64_t
#define all(x) x.begin(), x.end()
#define pb emplace_back
#define ff first
#define ss second
#define sz(x) (int)(x.size())
#define deb cout << "----------------------"
#define MIN(x) *min_element(all(x))
#define MINID(x) min_element(all(x)) - x.begin()
#define MAX(x) *max_element(all(x))
#define MAXID(x) max_element(all(x)) - x.begin()
#define push emplace
#define SUM(x) accumulate(all(x), 0ll)
#define sj(x) sort(all(x)), x.erase(unique(all(x)), x.end())

mt19937 rnd(221);

using ll = long long;
using vi = vector<int>;
using pi = pair<int,int>;
using vvi = vector<vi>;
using vpi = vector<pi>;
using vvpi = vector<vpi>;
using ld = long double;

const int64_t INF = 1e18;
const int INF2 = 1e9;
const int mod = 998244353;
const ld eps = 1e-6;

int bpow(int x, int st) {
    if (st == 0) return 1;
    int res = bpow(x, st/ 2);
    res = res * res; res %= mod;
    if (st % 2 == 1) res *= x;
    res %= mod;
    return res;
}

const int N = 1e5 + 2323;
const int D = 500;

void solve() {
    int n, q; cin >> n >> q;
    vi p(n); cin >> p;
    vi p2(n);
    auto calc = [&]() -> int {
        int su = 0;
        for (int i = 0; i < n; i++) {
            su += bpow(p[i], i + 1);
            if (su >= mod) su -= mod;
        }
        return su;
    };
    vi b(q); cin >> b;
    set<vi> st;
    for (auto x : b) {
        if (x == 1) {
            reverse(all(p));
        } else if (x == 2) {
            for (int i = 0; i < n; i++) {
                p[i] = n - p[i] + 1;
            }
        } else {
            for (int i = 0; i < n; i++) {
                p2[p[i] - 1] = i + 1;
            }
            p = p2;
        }
        //cout << p;
        st.insert(p);
        cout << calc() << ' ';
    }
    assert(sz(st) <= 7);
}

int32_t main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cout << fixed << setprecision(20);
    int tt = 1;
    // cin >> tt;
    while (tt--) {
        solve();
    }
}
