#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <set>

#define int long long

using namespace std;
const int MOD = 998244353;

int mul(int a, int b) {
    return (long long)a * b % MOD;
}

int bin_pow(int a, int n) {
    if (n == 0) return 1;
    if (n == 1) return a;
    if (n % 2 == 0) {
        int ax = bin_pow(a, n / 2);
        return mul(ax, ax);
    } else {
        return mul(a, bin_pow(a, n - 1));
    }
}

int add(int a, int b) {
    if (a + b >= MOD) return a - MOD + b;
    return a + b;
}

int get(int n, vector<int> &a) {
    int cur = 0;
    for (int i = 0; i < n; i++) {
        cur = add(cur, bin_pow(a[i], i + 1));
    }
    return cur;
}

map<int, string> res;
map<string, int> pld;
map<string, string> mp;

void rec(int n, vector<int> &a, string &st, int idx, int bar) {
    if (idx == bar) {
        int cur = 0;
        vector<int> ax = a;
        for (auto x : st) {
            if (x == '0') {
                reverse(ax.begin(), ax.end());
            } else if (x == '1') {
                for (auto &elem : ax) elem = n + 1 - elem;
            } else {
                vector<int> bx = ax;
                for (int i = 0; i < n; i++) {
                    bx[ax[i] - 1] = i + 1;
                }
                ax = bx;
            }
        }
        cur = get(n, ax);
        if (res.count(cur)) {
            mp[st] = res[cur];
        } else {
            res[cur] = st;
            pld[st] = cur;
        }
        return;
    }
    for (int i = 0; i < 3; i++) {
        char c = (char)('0' + i);
        st += c;
        rec(n, a, st, idx + 1, bar);
        st.pop_back();
    }
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, q;
    cin >> n >> q;
    vector<int> a(n);
    for (int i = 0; i < n; i++) cin >> a[i];
    string st = "";
    for (int i = 0; i <= 4; i++) {
        rec(n, a, st, 0, i);
    }
    for (int i = 0; i < q; i++) {
        char c;
        cin >> c;
        c--;
        st += c;
        if (mp.count(st)) {
            st = mp[st];
        }
        cout << pld[st] << " ";
    }
    return 0;
}
