#include <bits/stdc++.h>

using namespace std;

const int mod = 998244353;

#define int int64_t

int binpow(int a,int b)
{
    return (b == 0 ? 1 : (b%2 == 0 ? binpow((a*a)%mod,b/2) : (a*binpow(a,b-1))%mod));
}

int calc_ans(vector<int> & p)
{
    int ans = 0;
    for(int j = 0;j < p.size();++j)
    {
        ans = (ans + binpow(p[j],j+1))%mod;
    }
    return ans;
}

void make_operation(vector<int> & p,int k)
{
    if(k == 0)
    {
        reverse(p.begin(),p.end());
    }
    else if(k == 1)
    {
        for(int i = 0;i < p.size();++i)
        {
            p[i] = p.size() - p[i] + 1;
        }
    }
    else
    {
        vector<int> q(p.size());
        for(int i = 0;i < p.size();++i)
        {
            q[p[i]-1] = i+1;
        }
        p = q;
    }
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n,q;
    cin >> n >> q;
    vector<int> p(n);
    for(int i =0;i < n;++i)
    {
        cin >> p[i];
    }
    vector<vector<int>> pos(1,p);
    map<vector<int>,int> gg;
    map<int,int> ans;
    gg[p] = 1;
    ans[0] = calc_ans(p);
    vector<vector<int>> g(1,{-1,-1,-1});
    int x = 0;
    int pnt = 2;
    int cnt = 0;
    for(int i = 0;i < q;++i)
    {
        int k;
        cin >> k;
        k--;
        if(g[x][k] != -1)
        {
            x = g[x][k];
        }
        else
        {
            cnt++;
            if(cnt > 24)
            {
                assert(0);
            }
            p = pos[x];
            make_operation(p,k);
            if(gg[p] == 0)
            {
                pos.push_back(p);
                g.push_back({-1,-1,-1});
                g[x][k] = pnt-1;
                ans[pnt-1] = calc_ans(p);
                x = pnt-1;
                gg[p] = pnt++;
            }
            else
            {
                int t = gg[p]-1;
                g[x][k] = t;
                x = t;
            }
        }
        cout << ans[x] << "\n";
    }
}