#define _GLIBCXX_DEBUG
#pragma GCC optimize("O3")
#include<bits/stdc++.h>
#define int long long 

const int mod = 998244353;

int bin_pow_mod(int a,int n){
    if(n == 0){
        return 1;
    }
    if(n % 2 == 0){
        int tmp = bin_pow_mod(a,n / 2);
        return (tmp * tmp) % mod;
    }else{
        return (a * bin_pow_mod(a,n - 1)) % mod;
    }
}

int give(std::vector<int> p){
    int ans = 0;
    for(int i = 0;i < p.size();i++){
        ans +=bin_pow_mod(p[i],i + 1);
        ans %= mod;
    }
    return ans;
}

signed main(){
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    
    int n,q;
    std::cin>>n>>q;


    std::vector<int> p(n);
    for(int i = 0;i < n;i++){
        std::cin>>p[i];
    }
    int cur = give(p);


    std::vector<std::map<int,int>> cnt(3);
    std::map<int,std::vector<int>> help;
    help[cur] = p;

    std::vector<int> b(q);
    for(int i = 0;i < q;i++){
        std::cin>>b[i];

        b[i]--;
        if(cnt[b[i]].find(cur) == cnt[b[i]].end()){
          //  std::cout<<"djf"<<std::endl;
            std::vector<int> p = help[cur];
            if(b[i] == 0){
                std::reverse(p.begin(),p.end());
            }else if(b[i] == 1){
                std::vector<int> now(n);
                for(int j = 0;j < n;j++){
                    now[j] = n - p[j] + 1;
                }
                std::swap(p,now);

            }else{
                std::vector<int> now(n);
                for(int j = 0;j < n;j++){
                    now[p[j] - 1] = j + 1;
                }
                std::swap(p,now);
            }
            int ty = give(p);
            help[ty] = p;
            cnt[b[i]][cur] = ty;
        }
        cur = cnt[b[i]][cur];
        std::cout<<cur<<' ';
    }
   // assert(all.size() <= 8);
}