#include <iostream>
#include <vector>
#include <algorithm>
#include <array>
using namespace std;
const long long mod = 998244353;

inline long long stepen(long long a, long long b)
{
    long long res = 1, t = a;
    while (b)
    {
        if (b & 1)
            (res *= t) %= mod;
        (t *= t) %= mod;
        b >>= 1;
    }
    return res;
}

inline void rev(vector<int>& p)
{
    int n = p.size();
    for (int i = 0; i < (n >> 1); ++i)
        swap(p[i], p[n - 1 - i]);
}

inline void inv(vector<int>& p)
{
    int n = p.size();
    for (int i = 0; i < n; ++i)
        p[i] = n + 1 - p[i];
}

inline void obr(vector<int>& p)
{
    int n = p.size();
    vector<int> q(n);
    for (int i = 0; i < n; ++i)
        q[p[i] - 1] = i + 1;
    swap(p, q);
}

inline long long F(vector<int>& p)
{
    int n = p.size();
    long long res = 0;
    for (int i = 0; i < n; ++i)
        (res += stepen(p[i], i + 1)) %= mod;
    return res;
}

int main()
{
    ios::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>> allp(8, vector<int> (n));
    allp[0] = p;
    for (int i = 1; i < 8; ++i)
    {
        if (i & 4)
        {
            allp[i] = allp[i ^ 4];
            obr(allp[i]);
        }
        else if (i & 2)
        {
            allp[i] = allp[i ^ 2];
            inv(allp[i]);
        }
        else
        {
            allp[i] = allp[i ^ 1];
            rev(allp[i]);
        }
    }
    array<long long, 8> ans;
    for (int i = 0; i < 8; ++i)
        ans[i] = F(allp[i]);
    int mask = 0;
    while (q--)
    {
        int tt;
        cin >> tt;
        if (tt == 1)
        {
            if (mask & 4)
                mask ^= 2;
            else
                mask ^= 1;
        }
        else if (tt == 2)
        {
            if (mask & 4)
                mask ^= 1;
            else
                mask ^= 2;
        }
        else
            mask ^= 4;
        cout << ans[mask] << ' ';
    }
    return 0;
}
