//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>

using namespace std;
#define int long long
//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx2")

constexpr int mod = 998244353;
int st(int x,int n)
{
    if (n==0) return x%mod;
    int a = st(x,n-1);
    return ((a*a)%mod+mod)%mod;
}
int get_st(int x,int n)
{
    int ans=1;
    for (int i=30;i>=0;i--)
    {
        if ((1<<i)<=n)
        {
            int a = st(x,i);
            n -= (1<<i);
            ans = ((ans*a)%mod+mod)%mod;
        }
    }
    return ans;
}
signed main()
{
    int n,q;
    cin >> n >> q;
    vector<int> p(n);
    for (int i=0;i<n;i++)
    {
        cin >> p[i];
    }
    vector<int> b(q);
    for (int i=0;i<q;i++)
    {
        cin >> b[i];
    }
    vector<vector<int>> ans = {{0,0,0,0},{0,0,0,0}};
    for (int i=0;i<n;i++)
    {
        ans[0][0] = (ans[0][0]+get_st(p[i],i+1))%mod;
    }
    reverse(p.begin(),p.end());
    for (int i=0;i<n;i++)
    {
        ans[0][1] = (ans[0][1]+get_st(p[i],i+1))%mod;
    }
    for (int i=0;i<n;i++)
    {
        p[i] = n+1-p[i];
    }
    for (int i=0;i<n;i++)
    {
        ans[0][3] = (ans[0][3]+get_st(p[i],i+1))%mod;
    }
    reverse(p.begin(),p.end());
    for (int i=0;i<n;i++)
    {
        ans[0][2] = (ans[0][2]+get_st(p[i],i+1))%mod;
    }
    for (int i=0;i<n;i++)
    {
        p[i] = n+1-p[i];
    }
    vector<int> p1(n);
    for (int i=0;i<n;i++)
    {
        p1[p[i]-1] = i+1;
    }
    swap(p,p1);
    for (int i=0;i<n;i++)
    {
        ans[1][0] = (ans[1][0]+get_st(p[i],i+1))%mod;
    }
    reverse(p.begin(),p.end());
    for (int i=0;i<n;i++)
    {
        ans[1][1] = (ans[1][1]+get_st(p[i],i+1))%mod;
    }
    for (int i=0;i<n;i++)
    {
        p[i] = n+1-p[i];
    }
    for (int i=0;i<n;i++)
    {
        ans[1][3] = (ans[1][3]+get_st(p[i],i+1))%mod;
    }
    reverse(p.begin(),p.end());
    for (int i=0;i<n;i++)
    {
        ans[1][2] = (ans[1][2]+get_st(p[i],i+1))%mod;
    }
    int u1=0,u2=0;
    for (int i=0;i<q;i++)
    {
        if (b[i]==1)
        {
            u2 ^= 1;
        }
        if (b[i]==2)
        {
            u2 ^= 2;
        }
        if (b[i]==3)
        {
            u1 ^= 1;
            if (u2==1)
            {
                u2 = 2;
            }
            else if (u2==2)
            {
                u2 = 1;
            }
        }
        cout << ans[u1][u2] << "\n";
    }
}
