#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define pii pair<int, int>
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define ff first
#define ss second
#define pb push_back
#define eb emplace_back

//#define int long long

const int mod = 998244353;
vector<int> p;
vector<int> zp;
int n;

int mul(int a, int b) {
    return (1ll * a * b) % mod;
}

int add(int a, int b) {
    int c = a + b;
    if (c >= mod)c -= mod;
    return c;
}

int dif(int a, int b) {
    int c = a - b;
    if (c < 0)c += mod;
    return c;
}

int fastpow(int x, int pw) {
    if (pw == 0)return 1;
    if (pw == 1)return x;
    if (pw & 1)return mul(x, fastpow(x, pw - 1));
    int c = fastpow(x, pw / 2);
    return mul(c, c);
}

int get() {
    int ans = 0;
    for (int i = 0; i < n; i++) {
        ans = add(ans, fastpow(p[i], i + 1));
    }
    return ans;
}


int get(int a, int b, int c) {
    vector<int> q(n);
    for (int i = 0; i < n; i++) {
        q[i] = p[i];
    }
    vector<int> q1(n);

    if (a) {
        for (int i = 0; i < n; i++) {
            q1[i] = q[i];
        }
        for (int i = 0; i < n; i++) {
            q[i] = q1[n - i - 1];
        }
    }
    if (b) {
        for (int i = 0; i < n; i++) {
            q1[i] = q[i];
        }
        for (int i = 0; i < n; i++) {
            q[i] = n - q1[i] + 1;
        }
    }
    if (c) {
        for (int i = 0; i < n; i++) {
            q1[i] = q[i];
        }
        for (int i = 0; i < n; i++) {
            q[q1[i] - 1] = i + 1;
        }
    }
    if (0) {
        for (int i = 0; i < n; i++) {
            cout << q[i] << ' ';
        }
        cout << '\n';
    }
    int an = 0;
    for (int i = 0; i < n; i++) {
        an = add(an, fastpow(q[i], i + 1));
    }
  //  cout << an << '\n';
    return an;
}

void solve() {
    int q;
    cin >> n >> q;
    p.resize(n);
    zp.resize(q);

    for (int i = 0; i < n; i++) {
        cin >> p[i];
    }
    int mat[3][3][3];
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            for (int k = 0; k < 2; k++) {
                mat[i][j][k] = get(i, j, k);
            }
        }
    }


    for (int i = 0; i < q; i++) {
        cin >> zp[i];
    }
    int cnt1 = 0, cnt2 = 0, cnt3 = 0;
    bool f = false;
    for (int i = 0; i < q; i++) {
        if (zp[i] == 1) {
            cnt1++;
            f = true;
            cnt1 %= 2;
        } else if (zp[i] == 2) {
            cnt2++;
            f = true;
            cnt2 %= 2;
        } else {
            cnt3++;
            cnt3 %= 2;
        }
        //cout << cnt1 << ' ' << cnt2 << ' ' << cnt3 << ' ' << mat[cnt1][cnt2][cnt3] << '\n';
       // cout << mat[cnt1][cnt2][cnt3] << ' ';
        if (i > 0) {
            if (zp[i - 1] == 3 && cnt3 && f && zp[i] != 3) {
                cnt2 += 1;
                cnt2 %= 2;
                cnt1 += 1;
                cnt1 %= 2;
               // cout << i << ' ';
                zp[i] = 3;
            }
        }
        cout << mat[cnt1][cnt2][cnt3] << ' ';


    }
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int _ = 1;
    //cin >> _;
    while (_--) {
        solve();
    }
}
