#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
using ll = long long;
constexpr int maxn = 2e5 + 3, mod = 998244353, inf = 1e9;
constexpr ll big_inf = 1e16;
constexpr __int128_t bnf = 1e30;
mt19937 rng(122);

int rand_int(int l, int r) {
    return uniform_int_distribution<int>(l, r)(rng);
}

struct segtr {
    struct node {
        int sm;
    };

    int n{};
    vector<node> t;
    vector<int> a;

    node merge(node a, node b) {
        node c{};
        c.sm = a.sm + b.sm;
        return c;
    }

    void build(int v, int tl, int tr) {
        if (tl + 1 == tr) {
            t[v].sm = a[tl];
            return;
        }
        int tm = (tl + tr) / 2;
        build(2 * v, tl, tm);
        build(2 * v + 1, tm, tr);
        t[v] = merge(t[v * 2], t[v * 2 + 1]);
    }

    segtr(int sz, vector<int> &b) {
        a = b;
        int k = 1;
        while (k < sz) {
            k *= 2;
        }
        n = k;
        t.assign(2 * n + 1, {0});
        build(1, 1, n + 1);
    }

    node get(int v, int tl, int tr, int ql, int qr) {
        if (tl >= qr || ql >= tr) {
            return {0};
        }
        if (ql <= tl && tr <= qr) {
            return t[v];
        }
        int tm = (tl + tr) / 2;
        return merge(get(v * 2, tl, tm, ql, qr),
                     get(v * 2 + 1, tm, tr, ql, qr));
    }

    void upd(int v, int tl, int tr, int ps, int x) {
        if (ps < tl || ps >= tr)
            return;
        if (tl + 1 == tr) {
            t[v].sm = x;
            return;
        }
        int tm = (tl + tr) / 2;
        upd(v * 2, tl, tm, ps, x);
        upd(v * 2 + 1, tm, tr, ps, x);
        t[v] = merge(t[v * 2], t[v * 2 + 1]);
    }
};

ll pod(ll x, int n) {
    if (n == 0) {
        return 1;
    }
    if (n % 2 == 0) {
        ll rs = pod(x, n / 2);
        return (rs * rs) % mod;
    }
    else {
        return (pod(x, n - 1) * x) % mod;
    }
}
vector<int> a1(vector<int> a) {
    int n = (int)a.size() - 1;
    vector<int> p(a.size());
    for (int i = 1; i <= n; i++) {
        //cout << n - i + 1 << endl;
        p[i] = a[n - i + 1];
    }
    return p;
}
vector<int> a2(vector<int> a) {
    int n = (int)a.size() - 1;
    vector<int> p(a.size());
    for (int i = 1; i <= n; i++) {
        p[i] = n - a[i] + 1;
    }
    return p;
}
vector<int> a3(vector<int> a) {
    int n = (int)a.size() - 1;
    vector<int> p(a.size());
    for (int i = 1; i <= n; i++) {
        p[a[i]] = i;
    }
    return p;
}
ll rz(vector<int> a) {
    ll res = 0;
    for (int i = 1; i < a.size();i++) {
        res += pod(a[i], i);
    }
    return res;
}
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 n, q;
    cin >> n >> q;
    vector<int> a(n + 1);
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    vector<ll> rs(4);
    rs[0] = rz(a);
    a = a1(a);
    rs[1] = rz(a);
    a = a1(a);
    a = a2(a);
    rs[2] = rz(a);
    a = a2(a);
    a = a3(a);
    rs[3] = rz(a);
    a = a3(a);
    bool fl = false;
    for (int i = 1; i <= q; i++) {
        int c;
        cin >> c;
        if (!fl) {
            cout << rs[0];
            fl = true;
        } else {
            fl = false;
            cout << rs[c];
            if (c == 3) {
                fl = true;
            }
        }
        cout << ' ';
    }
}