#define _GLIBCXX_DEBUG
#include<bits/stdc++.h>

using namespace std;

using ll = long long;

#define f first
#define s second
#define pii pair<ll, ll>

ll const MOD = 998244353;

ll sum(ll a, ll b) {
    return (a + b) % MOD;
}

ll mult(ll a, ll b) {
    return a * b % MOD;
}

ll bin_pow(ll a, ll p) {
    if (p == 0) return 1;
    if (p == 1) return a;
    ll k = bin_pow(a, p / 2);
    return mult(mult(k, k), bin_pow(a, p % 2));
}

ll calc(vector<ll> &p) {
    ll res = 0;
    for (ll i = 0; i < p.size(); ++i) {
        res = sum(res, bin_pow(p[i], i + 1));
    }
    return res;
}

vector<ll> change1(vector<ll> p) {
    vector<ll> res = p;
    reverse(res.begin(), res.end());
    return res;
}


vector<ll> change2(vector<ll> p) {
    vector<ll> res = p;
    for (int i = 0; i < res.size(); ++i) {
        res[i] = res.size() - res[i] + 1;
    }
    return res;
}


vector<ll> change3(vector<ll> p) {
    vector<ll> res = p, buf = p;
    for (int i = 0; i < res.size(); ++i) {
        res[buf[i] - 1] = i + 1;
    }
    return res;
}








int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll n, q;
    cin >> n >> q;
    vector<ll> p(n), b(q);
    for (ll i = 0; i < n; ++i) {
        cin >> p[i];
    }
    for (ll i = 0; i < q; ++i) {
        cin >> b[i];
    }

    map<vector<int>, int> mp;

    vector<ll> p1 = change1(p);
    vector<ll> p2 = change2(p);
    vector<ll> p3 = change3(p);

    vector<ll> res(4);

    ll a0 = calc(p);
    mp[{}] = a0;
    //res[0].push_back(a0);

    ll a1 = calc(p1);
    ll a2 = calc(p2);
    ll a3 = calc(p3);

    mp[{1}] = a1;
    mp[{2}] = a2;
    mp[{3}] = a3;

//    res[1].push_back(a1);
//    res[1].push_back(a2);
//    res[1].push_back(a3);

    vector<ll> p12 = change2(p1);
    vector<ll> p21 = change1(p2);
    vector<ll> p13 = change3(p1);
    vector<ll> p31 = change1(p3);
    vector<ll> p23 = change3(p2);
    vector<ll> p32 = change2(p3);

    ll a12 = calc(p12);
    ll a21 = calc(p21);
    ll a13 = calc(p13);
    ll a31 = calc(p31);
    ll a23 = calc(p23);
    ll a32 = calc(p32);

    mp[{1, 2}] = a12;
    mp[{2, 1}] = a21;
    mp[{1, 3}] = a13;
    mp[{3, 1}] = a31;
    mp[{2, 3}] = a23;
    mp[{3, 2}] = a32;

//    res[2].push_back(a12);
//    res[2].push_back(a21);
//    res[2].push_back(a13);
//    res[2].push_back(a31);
//    res[2].push_back(a23);
//    res[2].push_back(a32);

    vector<ll> p123 = change3(p12);
    vector<ll> p132 = change2(p13);
    vector<ll> p213 = change3(p21);
    vector<ll> p231 = change1(p31);
    vector<ll> p312 = change2(p12);
    vector<ll> p321 = change1(p21);

    ll a123 = calc(p123);
    ll a132 = calc(p132);
    ll a213 = calc(p213);
    ll a231 = calc(p231);
    ll a312 = calc(p312);
    ll a321 = calc(p321);

    mp[{1, 2, 3}] = a123;
    mp[{1, 3, 2}] = a132;
    mp[{2, 1, 3}] = a213;
    mp[{2, 3, 1}] = a231;
    mp[{3, 1, 2}] = a312;
    mp[{3, 2, 1}] = a321;

    //exit(0);


    vector<int> ord;


    for (int i = 0; i < q; ++i) {
        int flag = 0;
        for (int j = 0; j < ord.size(); ++j) {
            if (ord[j] == b[i]) {
                flag = 1;
                ord.erase(ord.begin() + j);
                if (j == ord.size() - 1) flag = 2;
                break;
            }
        }
        if (!flag) {
            ord.push_back(b[i]);
        } else if (flag != 1) {
            reverse(ord.begin(), ord.end());
        }
        //reverse(ord.begin(), ord.end());
//        if (!flag && b[i] == 3) {
//            reverse(ord.begin(), ord.end());
//        }
        cout << mp[ord] << " ";
//        if (!flag && b[i] == 3) {
//            reverse(ord.begin(), ord.end());
//        }
        //reverse(ord.begin(), ord.end());
    }







}

