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

#pragma GCC optimize("03")
#pragma GCC optimize("unroll-loops")

#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define int long long

using pii = pair <int, int>;

const int mod = 998244353;

int bin_pow(int x, int n) {
    if (n == 0) return 1 % mod;
    if (n % 2 == 0) return bin_pow(x * x % mod, n / 2);
    return x * bin_pow(x, n - 1) % mod;
}

void add(int &x, int y) {
    x += y;
    if (x >= mod) x -= mod;
}

void del(int &x, int y) {
    x -= y;
    if (x < 0) x += mod;
}

int calc(int n, const vector <int> &v) {
    int ans = 0;
    for (int i = 0; i < n; ++i) {
        add(ans, bin_pow(v[i], i + 1));
    }
    return ans;
}

vector <int> op(int n, const vector <int> &a, int type) {
    vector <int> ans(n);
    if (type == 1) {
        for (int i = 0; i < n; ++i) ans[i] = a[n - i - 1];
    }
    if (type == 2) {
        for (int i = 0; i < n; ++i) ans[i] = n - a[i] + 1;
    }
    if (type == 3) {
        for (int i = 0; i < n; ++i) ans[a[i] - 1] = i + 1;
    }
    return ans;
}

signed main() {
    // ios_base::sync_with_stdio(false);
    // cin.tie(0);
    // cout.tie(0);
    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> ans;
    int mem = calc(n, a);
    for (int i = 1; i <= 3; ++i) ans[{i}] = calc(n, op(n, a, i));
    for (int i = 1; i <= 3; ++i) {
        for (int j = 1; j <= 3; ++j) {
            ans[{i, j}] = calc(n, op(n, op(n, a, i), j));
        }
    }
    for (int i = 1; i <= 3; ++i) {
        for (int j = 1; j <= 3; ++j) {
            for (int k = 1; k <= 3; ++k) {
                ans[{i, j, k}] =  calc(n, op(n, op(n, op(n, a, i), j), k));
            }
        }
    }
    vector <int> buf;
    for (int i = 0; i < q; ++i) {
        int have = 0;
        if ((int)buf.size() > 0 && buf[0] == b[i]) {
            buf.erase(buf.begin());
            have = 1;
        }
        if ((int)buf.size() > 1 && buf[1] == b[i]) {
            buf.erase(buf.begin() + 1);
            have = 1;
        }
        if ((int)buf.size() > 2 && buf[2] == b[i]) {
            buf.erase(buf.begin() + 2);
            have = 1;
        }
        if (!have) buf.emplace_back(b[i]);
        cout << (buf.empty() ? mem : ans[buf]) << ' ';
    }

}