#include <bits/stdc++.h>

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("fast-math")
#pragma GCC optimize("no-stack-protector")

//#define D_GLIBCXX_DEBUG
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pii pair<int, int>

typedef long long ll;
typedef long double ld;

#define int long long

using namespace std;

//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

const int mod = 998244353;
const int inf = 1e9 + 9;


void rev(vector<int> & a) {
    vector<int> q = a;
    int n = a.size();
    for (int i = 0; i < n; i++) q[i] = a[n - i - 1];
    a = move(q);
    return;
}
void per(vector<int> & a) {
    vector<int> q = a;
    int n = a.size();
    for (int i = 0; i < n; i++) q[i] = n - a[i] + 1;
    a = move(q);
    return;
}
void obr(vector<int> & a) {
    vector<int> q = a;
    int n = a.size();
    for (int i = 0; i < n; i++) q[a[i] - 1] = i + 1;
    a = move(q);
    return;
}

ll pw(int a, int p) {
    if (p == 0) return 1;
    if (p % 2) return (a * pw(a, p - 1)) % mod;
    int x = pw(a, p / 2);
    return (x * x) % mod;
}

ll cost(vector<int> & a) {
    ll ans = 0;
    int n = a.size();
    for (int i = 0; i < n; i++) {
        ans += pw(a[i], i + 1);
        ans %= mod;
    }
    return ans;
}

void solve() {
    int n, q;
    cin >> n >> q;
    vector<int> p(n), b(q);
    for (auto &i: p) cin >> i;
    vector<int> cst(8);
    for (int i = 0; i < 8; i++) {
        vector<int> a = p;
        if (i & 1) rev(a);
        if ((i >> 1) & 1) per(a);
        if ((i >> 2) & 1) obr(a);
        for (auto j:a) cout << j << ' ';
        cout << '\n';
        cst[i] = cost(a);
        cout << i << ' ' << cst[i] << '\n';
    }
    int cr = 0;
    for (auto &i: b) {
        cin >> i;
        if (i == 1) {
            if (cr == 7) cr = 5;
            else if (cr == 5) cr = 7;
            else cr ^= 1;
        }
        if (i == 2) cr ^= 2;
        if (i == 3) cr ^= 4;
        cout << cst[cr] << ' ';
    }
}

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
#ifdef Local
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif // Local
    int t = 1;
    //cin >> t;
    while (t--) {
        solve();
    }
}
