#include <iostream>
#include <vector>
#include <unordered_set>
#include <iomanip>
#include <cmath>
#include <map>
#include <set>
#include <deque>
#include <random>
#include <algorithm>
#include <unordered_map>

using namespace std;
using ll = long long int;
using ld = long double;
using ull = unsigned long long int;
using uint = unsigned int;

const ll linf = 1e18 + 7;
const int inf = 1e9 + 7;
const ll M = 998244353;
const ld PI = 2 * asin(1);

#define sz(v) (int)v.size()
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend();
#define nr nullptr

ll binpow(ll x, ll k)
{
    if (k == 0)
    {
        return 1;
    }
    else if (k % 2 == 0)
    {
        return binpow((x * x) % M, k / 2);
    }
    else
    {
        return (binpow(x, k - 1) * x) % M;
    }
}

vector <ll> a;
vector <ll> b;

ll get_sum(vector<ll>& c)
{
    ll s = 0;
    for (int i = 0; i < sz(c); i++)
    {
        s = (s + binpow(c[i], i + 1)) % M;
    }
    return s;
}

vector <ll> c(4);

void solve()
{
    int n, q;
    cin >> n >> q;
    a.resize(n);
    b.resize(n);
    for (auto& i : a) cin >> i;
    b = a;
    reverse(all(b));
    c[0] = get_sum(a);
    c[1] = get_sum(b);
    for (int i = 0; i < n; i++)
    {
        b[i] = n + 1 - a[i];
    }
    c[2] = get_sum(b);
    reverse(all(b));
    c[3] = get_sum(b);
    
    int cur = 0;
    
    for (int i = 0; i < q; i++)
    {
        int t = 0;
        cin >> t;
        if (cur == 0)
        {
            if (t == 1)
            {
                cur = 1;
            }
            else
            {
                cur = 2;
            }
        }
        else if (cur == 1)
        {
            if (t == 1)
            {
                cur = 0;
            }
            else
            {
                cur = 3;
            }
        }
        else if (cur == 2)
        {
            if (t == 1)
            {
                cur = 3;
            }
            else
            {
                cur = 0;
            }
        }
        else if (cur == 3)
        {
            if (t == 1)
            {
                cur = 2;
            }
            else
            {
                cur = 1;
            }
        }
        cout << c[cur] << ' ';
    }

}

int main()
{
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    //freopen("input.txt", "r", stdin);

    int _ = 1;
    while (_--)
    {
        solve();
        cout << '\n';
    }
}
