#include <bits/stdc++.h>

using namespace std;
using ll = long long;
#define int ll

using pii = pair<int, int>;

const int MOD = 998244353;
struct modint{
    int val;
    modint(int x){
        if(x >= MOD){
            x %= MOD;
        }
        val = x;
    }
    modint(){
        val = 0;
    }
};

modint operator+(modint a, modint b){
    return a.val + b.val - (a.val + b.val >= MOD ? MOD : 0);
}

modint operator-(modint a, modint b){
    return a.val - b.val + (a.val - b.val < 0 ? MOD : 0);
}

modint operator*(modint a, modint b){
    return ((ll)a.val * b.val) % MOD;
}

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

modint inv(modint a){
    return bpow(a, MOD - 2);
}

modint operator/(modint a, modint b){
    return a * inv(b);
}

vector<int> rev(const vector<int> &p){
    vector<int> q(p.size());
    for(int i = 0; i < q.size(); i++){
        q[i] = p[q.size() - i - 1];
    }
    return q;
}

vector<int> inv(const vector<int> &p){
    vector<int> q(p.size());
    for(int i = 0; i < q.size(); i++){
        q[i] = q.size() - p[i] + 1;
    }
    return q;
}

vector<int> obr(const vector<int> &p){
    vector<int> q(p.size());
    for(int i = 0; i < q.size(); i++){
        q[p[i] - 1] = i + 1;
    }
    return q;
}   

modint ans(const vector<int> &p){
    modint res;
    for(int i = 0; i < p.size(); i++){
        res = res + bpow(modint(p[i]), i + 1);
    }
    return res;
}

void solve(){
    int n, q;
    cin >> n >> q;
    vector<int> p(n);
    for(auto &x : p){
        cin >> x;
    }
    cout << ans(p).val << '\n';
    unordered_map<int, vector<int>> mp;
    unordered_map<int, unordered_map<int, int>> go;
    int hsh = ans(p).val;
    mp[hsh] = p;
    while(q--){
        int t;
        cin >> t;
        if(go[hsh].count(t)){
            hsh = go[hsh][t];
            cout << hsh << '\n';
            continue;
        }
        auto q = mp[hsh];
        vector<int> q2;
        int hsh2;
        if(t == 1){
            q2 = rev(q);
            hsh2 = ans(q2).val;
        }
        if(t == 2){
            q2 = inv(q);
            hsh2 = ans(q2).val;
        }
        if(t == 3){
            q2 = obr(q);
            hsh2 = ans(q2).val;
        }
        
        mp[hsh2] = q2;
        go[hsh][t] = hsh2;
        hsh = hsh2;
        cout << hsh << ' ';
    }
    cout << '\n';
}

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