#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define pair pair<int, int>
#define int long long
char buf[1 << 20], *_now = buf, *_end = buf;
#define getchar() (_now == _end && (_end = (_now = buf) + fread(buf, 1, 1 << 20, stdin), _now == _end) ? EOF : *_now++)
int n, q, x;
long long pd = 1;
char opt;
multiset<int> st;
inline int read() {
int p = 0;
char c = getchar();
for (; c < '0' || c > '9'; c = getchar());
for (; c >= '0' && c <= '9'; p = (p << 3) + (p << 1) + (c ^ 48), c = getchar());
return p;
}
long long qpow(long long a, long long b) {
long long res = 1;
while (b) {
if (b & 1) res = res * a % 317847191;
a = a * a % 317847191;
b >>= 1;
}
return res;
}
signed main() {
cin.tie(0), cout.tie(0);
ios::sync_with_stdio(false);
n = read(), q = read();
for (int i = 1; i <= n; i++) cin >> x, st.insert(x), pd = pd * (x % 317847191) % 317847191;
while (q--) {
cin >> opt;
if (opt == 'D') {
cin >> x;
auto pos = st.lower_bound(x);
st.erase(pos);
pd /= x;
}
else if (opt == 'B') {
auto p = st.end();
p--;
cout << *p << endl;
}
else if (opt == 'S') {
auto p = st.begin();
cout << *p << endl;
}
else if (opt == 'M') {
auto p = st.end();
p--;
cout << qpow(*p, *st.begin()) << endl;
}
else if (opt == 'T'){
cout << pd % 317847191 << endl;
}
}
return 0;
}