// #include "guess.h"
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define int long long

using namespace __gnu_pbds;
using namespace std;

template<typename T>
using oset = tree<
    T,
    null_type,
    less<>,
    rb_tree_tag,
    tree_order_statistics_node_update
    >;

struct RSQ {
    vector<int> segtree, push;
    int n;

    RSQ(int _n) {
        n = _n;
        segtree.assign(4 * n, 0);
        push.assign(4 * n, 0);
    }

    void make_push(int v, int l, int r) {
        int mid = (l + r) >> 1;
        push[v << 1] += push[v];
        push[v << 1 | 1] += push[v];
        segtree[v << 1] += push[v] * (mid - l + 1);
        segtree[v << 1 | 1] += push[v] * (r - (mid + 1) + 1);
        push[v] = 0;
    }

    void add(int ql, int qr, int val, int v = 1, int l = 0, int r = -1) {
        if (r == -1) {
            r = n - 1;
        }
        if (qr < l || ql > r) {
            return;
        }
        if (ql <= l && r <= qr) {
            push[v] += val;
            return;
        }
        make_push(v, l, r);
        int mid = (l + r) >> 1;
        add(ql, qr, val, v << 1, l, mid);
        add(ql, qr,  val, v << 1 | 1, mid + 1, r);
        segtree[v] += segtree[v << 1] + segtree[v << 1 | 1];
    }

    int getSum(int ql, int qr, int v = 1, int l = 0, int r = -1) {
        if (r == -1) {
            r = n - 1;
        }
        if (qr < l || ql > r) {
            return 0;
        }
        if (ql <= l && r <= qr) {
            return segtree[v];
        }
        make_push(v, l, r);
        int mid = (l + r) >> 1;
        return getSum(ql, qr, v << 1, l, mid) + getSum(ql, qr, v << 1 | 1, mid + 1, r);
    }
};

struct RMQ {
    vector<int> segtree, push;
    int n;

    RMQ(int _n) {
        n = _n;
        segtree.assign(4 * n, 1e18);
        push.assign(4 * n, -1);
    }

    void make_push(int v) {
        if (push[v] != -1) {
            segtree[v << 1] = segtree[v << 1 | 1] = push[v << 1] = push[v << 1 | 1] = push[v];
        }
        push[v] = -1;
    }

    void change(int ql, int qr, int to, int v = 1, int l = 0, int r = -1) {
        if (r == -1) {
            r = n - 1;
        }
        if (qr < l || ql > r) {
            return;
        }
        if (ql <= l && r <= qr) {
            push[v] = to;
            segtree[v] = to;
            return;
        }
        make_push(v);
        int mid = (l + r) >> 1;
        change(ql, qr, to, v << 1, l, mid);
        change(ql, qr, to, v << 1 | 1, mid + 1, r);
        segtree[v] = min(segtree[v << 1], segtree[v << 1 | 1]);
    }

    int getMin(int ql, int qr, int v = 1, int l = 0, int r = -1) {
        if (r == -1) {
            r = n - 1;
        }
        if (qr < l || ql > r) {
            return 1e18;
        }
        if (ql <= l && r <= qr) {
            return segtree[v];
        }
        int mid = (l + r) >> 1;
        return min(getMin(ql, qr, v << 1, l, mid), getMin(ql, qr, v << 1 | 1, mid + 1, r));
    }
};

const int LOG = 20 + 1;

struct SparseTable {
    vector<vector<int> > sparse;

    SparseTable(int n, vector<int> &from) {
        sparse.assign(20, vector<int>(n));
        sparse[0] = from;
        int j, i;
        for (j = 1; j < LOG; j++) {
            for (i = 0; i + (1 << j) <= n; i++) {
                sparse[j][i] = min(sparse[j - 1][i], sparse[j - 1][i + (1 << (j - 1))]);
            }
        }
    }

    int get(int l, int r) {
        int lg = log2(r - l + 1);
        return min(sparse[lg][l], sparse[lg][r - (1 << lg) + 1]);
    }
};

const int MAXN = 1e5 + 1;
// vector<int> adj[MAXN];
// int tin[MAXN], tout[MAXN];

// int timer = 0;
// void dfs1(int v, int p = -1) {
//     tin[v] = timer;
//     for (auto &e : adj[v]) {
//         if (e != p) {
//             timer++;
//             dfs1(e, v);
//         }
//     }
//     tout[v] = timer;
// }

map<vector<int>, int> toCode;
map<int, vector<int> > fromCode;
map<int, int> priceOfCode;
map<int, map<int, int> > adj;

int state[MAXN];

queue<int> qu;

const int MOD = 998244353;
int binpow(int a, int x) {
    if (x == 0) {
        return 1;
    }
    int res = binpow(a, x / 2);
    if (x % 2) {
        return res * res % MOD * a % MOD;
    }
    return res * res % MOD;
}

int timer = 0;
int n, q;
int getCode(vector<int> &elems) {
    auto it = toCode.find(elems);
    if (it != toCode.end()) {
        return it->second;
    }
    int i, sum = 0;
    for (i = 0; i < n; i++) {
        sum += binpow(elems[i], i + 1);
        sum %= MOD;
    }
    toCode[elems] = timer;
    fromCode[timer] = elems;
    priceOfCode[timer] = sum;
    return timer++;
}

void addConnection(int a, int b, int type) {
    adj[a][type] = b;
    if (state[b] == 0) {
        qu.emplace(b);
    }
}

vector<int> elems, elems1;
void inverse(vector<int> &elems) {
    int i;
    for (i = 0; i < n; i++) {
        elems1[elems[i] - 1] = i + 1;
    }
    elems = elems1;
}

void makeConnections(int code) {
    elems = fromCode[code];
    int i;
    int otherCode;
    // Развернуть
    reverse(begin(elems), end(elems));
    otherCode = getCode(elems);
    reverse(begin(elems), end(elems));
    addConnection(code, otherCode, 1);
    // Перевернуть
    for (i = 0; i < n; i++) {
        elems[i] = n - elems[i] + 1;
    }
    otherCode = getCode(elems);
    for (i = 0; i < n; i++) {
        elems[i] = n - elems[i] + 1;
    }
    addConnection(code, otherCode, 2);
    // Обратно
    inverse(elems);
    otherCode = getCode(elems);
    inverse(elems);
    addConnection(code, otherCode, 3);

    state[code] = 1;
}

void solve() {
    cin >> n >> q;
    elems1.resize(n);

    vector<int> per(n);
    int i, j;
    for (i = 0; i < n; i++) {
        cin >> per[i];
    }

    getCode(per);
    qu.emplace(0);
    while (qu.size()) {
        int curCode = qu.front();
        qu.pop();
        makeConnections(curCode);
    }

    int currentCode = getCode(per);
    while (q--) {
        int b;
        cin >> b;
        currentCode = adj[currentCode][b];
        cout << priceOfCode[currentCode] << ' ';
    }
}

signed main() {
    int t = 1;
    // cin >> t;

    while (t--) {
        solve();
    }
}