#include <iostream>
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;
using lll = __int128;
using pii = pair <int, int>;
template <typename T>
using vec = vector <T>;

#define all(x) x.begin(), x.end()

template <typename T>
istream& operator >>(istream& in, vec <T>& a) {
    for (auto& x : a) in >> x;
    return in;
}
template <typename T>
ostream& operator <<(ostream& in, vec <T>& a) {
    for (auto& x : a) in << x << " ";
    return in;
}
template <typename T, typename Y>
istream& operator >>(istream& in, pair <T, Y>& a) {
    in >> a.first >> a.second;
    return in;
}
template <typename T, typename Y>
ostream& operator <<(ostream& in, pair <T, Y>& a) {
    in << a.first << " " << a.second;
    return in;
}
void FAST() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
}

const int INF = 1e9 + 7;
const ll LLINF = 1e18 + 7;
const ll MOD = 998'244'353;

ll binpow(ll val, ll st) {
    if (st == 0) return 1;
    ll a = binpow(val, st / 2);
    a = (a * a) % MOD;
    if (st % 2) a = (a * val) % MOD;
    return a;
}

ll coun(vec <int>& a) {
    ll ans = 0;
    int n = a.size();
    for (int i = 0; i < n; i++) {
        ans = (ans + binpow(a[i], i + 1)) % MOD;
    }
    return ans;
}
void inv(vec <int>& a) {
    int n = a.size();
    for (int i = 0; i < n; i++) a[i] = n - a[i] + 1;
}
void hu(vec <int>& a) {
    vec <int>b(a.size());
    for (int i = 0; i < a.size(); i++) b[a[i] - 1] = i + 1;
    a = b;
}

void change(int& fl, int b) {
    if (fl == 1) {
        if (b == 1) fl = 2;
        else if (b == 2) fl = 2;
        else if (b == 3) fl = 1;
    }
    else if (fl == 2) {
        if (b == 1) fl = 1;
        else if (b == 2) fl = 1;
        else if (b == 3) fl = 1;
    }
   /* else if (fl == 3) {
        if (b == 1) fl = ;
        else if (b == 2) fl = ;
        else if (b == 3) fl = 1;
    }
    else if (fl == 4) {
        if (b == 1) fl = ;
        else if (b == 2) fl = ;
        else if (b == 3) fl = 1;
    }*/

}

int main() {
    FAST();

    int n, q;
    cin >> n >> q;
    vec <int> a(n), b(q);
    cin >> a >> b;

    //1
    vec <int> ra = a;//2
    reverse(all(ra));

    /*
    vec <int> ia = a;//3
    inv(a);

    vec <int> ira = ra;//4
    inv(ira);*/

    vec <ll> ans = {0, coun(a), coun(ra)}; //coun(ia), coun(ira)};
   // cout << "ans: " << ans << "\n";

    int fl = 1;
    for (int u = 0; u < q; u++) {
        int bi = b[u];
        //cout << "fl: " << fl << " -> ";
        change(fl, bi);
        //cout << fl << endl;
        cout << ans[fl] << " ";
        //cout << "res: " << ans[fl ]<< endl;
    }
}