#include <bits/stdc++.h>

#define int long long
#define all(a) a.begin(), a.end()
#define pii pair <int, int>
#define fr first
#define se second
#define uwu ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

using namespace std;

const int maxn = 1e5 + 5;
const int mod9 = 998244353;

int bin_pow(int a, int b){
    if (b == 0){
        return 1;
    }
    if (b & 1){
        return a * bin_pow(a, b - 1)  % mod9;
    }
    int k = bin_pow(a, b / 2);
    return k * k % mod9;
}

void solve(){
    int n, q;
    cin >> n >> q;
    vector <int> a(n), b(q);
    for (int i = 0; i < n; ++i){
        cin >> a[i];
    }
    for (int i = 0; i < q; ++i){
        cin >> b[i];
    }
    map <vector <int>, int> mp;
    vector <int> d;
    for (int i = 0; i < q; ++i){
        if (d.size() && d.back() == b[i]){
            d.pop_back();
        }
        else{
            d.push_back(b[i]);
        }
        if (d.size() > 3){
            vector <int> h;
            for (int i = 1; i < d.size(); ++i){
                h.push_back(d[i]);
            }
            d = h;
        }
        if (mp.find(d) != mp.end()){
            cout << mp[d] << ' ';
        }
        else{
            vector <int> h = a;
            for (int j = 0; j < d.size(); ++j){
                if (d[j] == 1){
                    for (int k = 0; k < n; ++k){
                        h[k] = h[n - k - 1];
                    }
                }
                else{
                    for (int k = 0; k < n; ++k){
                        h[k] = n - h[k] + 1;
                    }
                }
            }
            int cnt = 0;
            for (int j = 0; j < n; ++j){
                cnt += bin_pow(h[j], j + 1);
                cnt %= mod9;
            }
            mp[d] = cnt;
            cout << cnt << ' ';
        }
    }
}

int32_t main(){

    int test = 1;
    //cin >> test;
    while (test--){
        solve();
    }
}
