//#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),b(q);
    for (int i=0;i<n;i++)
    {
        cin >> p[i];
    }
    for (int i=0;i<q;i++)
    {
        cin >> b[i];
    }
    int ans1=0,ans2=0;
    for (int j=0;j<n;j++)
    {
        ans1 = (ans1+get_st(p[j],j+1))%mod;
    }
    if (b[0]==1)
    {
        reverse(p.begin(),p.end());
        for (int j=0;j<n;j++)
        {
            ans2 = (ans2+get_st(p[j],j+1))%mod;
        }
    }
    else if (b[0]==2)
    {
        for (int i=0;i<n;i++)
        {
            p[i] = n+1-p[i];
        }
        for (int j=0;j<n;j++)
        {
            ans2 = (ans2+get_st(p[j],j+1))%mod;
        }
    }
    else
    {
        vector<int> p1(n);
        for (int i=0;i<n;i++)
        {
            p1[p[i]-1] = i+1;
        }
        swap(p,p1);
        for (int j=0;j<n;j++)
        {
            ans2 = (ans2+get_st(p[j],j+1))%mod;
        }
    }
    int u=1;
    for (int i=0;i<q;i++)
    {
        u ^= 1;
        if (u)
        {
            cout << ans1 << "\n";
        }
        else
        {
            cout << ans2 << "\n";
        }
    }
}
