#P2174. 小Z的神奇数列

小Z的神奇数列

Description

You need to maintain a multiset that supports five operations:

  • D x Delete xx; it is guaranteed that xx exists. If there are multiple occurrences, delete only one.
  • B Query the maximum value in the set.
  • S Query the minimum value in the set.
  • M Let the maximum be aa and the minimum be bb; query abmod317847191a^b \bmod 317847191.
  • T Query the product of all numbers, modulo 317847191317847191.

For all queries, the multiset is guaranteed to be non-empty.

Input Format

The first line contains two positive integers n,mn, m, the initial multiset size and the number of operations.
The second line contains nn positive integers aia_i, the initial multiset.
Then mm lines follow, each describing one operation.

Output Format

For each query, output one integer per line representing the answer.

3 6
2 6 9
M
D 9
B
S
M
T
81
6
2
36
12

Hint

Constraints
For partial testdata, 1n10001 \le n \le 1000, 1m1001 \le m \le 100, 1ai4001 \le a_i \le 400.
For 100%100\% of the testdata, 1n,m1061 \le n, m \le 10^6, 1ai1081 \le a_i \le 10^8.

Translated by ChatGPT 5