#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#ifdef LOCAL
#define _GLIBCXX_DEBUG
#else
#pragma GCC optimize("O3,Ofast")
#pragma GCC target("avx2")
#endif

using namespace std;
using namespace __gnu_pbds;

using ll = long long;
using ld = long double;

using oset = tree<int, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update>;
using hash_table = gp_hash_table<int, int>;

void solve();

int main() {
#ifndef LOCAL
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
#else
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);

    auto timer = chrono::high_resolution_clock().now().time_since_epoch().count()/1000;
    cout << "DEBUG TIMER: ";
    int ss = timer / 1000000;
    int mm = ss / 60;
    int hh = mm / 60 + 3;
    cout << hh % 24 << ':' << mm % 60 << ':' << ss % 60 << "\n";
#endif
    int tt = 1;
    // cin >> tt;
    while (tt--) {
        solve();
    }
    return 0;
}

#define int ll

constexpr int mod = 998244353;

struct mint {
    int x = 0;
    mint() = default;
    mint(int x) : x(x) {}
    mint operator+(const mint b) const {
        return x + b.x < mod ? x + b.x : x + b.x - mod;
    }
    mint operator-(const mint b) const {
        return x - b.x < 0 ? x - b.x + mod : x - b.x;
    }
    mint operator*(const mint b) const {
        return 1ll * x * b.x % mod;
    }
    mint operator^(int n) const {
        mint ans = 1, base = x;
        while (n) {
            if (n & 1) ans = ans * base;
            base = base * base;
            n >>= 1;
        }
        return ans;
    }
    mint inv() const {
        return operator^(mod - 2);
    }
    mint operator/(const mint b) const {
        return operator*(b.inv());
    }
};

int get(vector<int>& p, const int rev, const int rot) {
    mint ans = 0;
    const int n = (int)p.size();
    for (int i = 0; i < n; ++i) {
        int x = p[i], deg = i + 1;
        if (rev) x = n + 1 - x;
        if (rot) deg = n + 1 - deg;
        ans = ans + (mint(x) ^ deg);
    }
    return ans.x;
}

inline void solve() {
    int n, q;
    cin >> n >> q;
    vector<int> p(n);
    for (int i = 0; i < n; ++i) {
        cin >> p[i];
    }
    map<pair<int, int>, int> prec;
    int rev = 0, rot = 0;
    while (q--) {
        int b;
        cin >> b;
        if (b == 1) rev ^= 1;
        if (b == 2) rot ^= 1;
        if (!prec[{rev, rot}])
            prec[{rev, rot}] = get(p, rev, rot) + 1;
        cout << prec[{rev, rot}] - 1 << ' ';
    }
}



