Data Structures Master
시간 제한2초메모리 제한2048 MB
세 수열 중 하나에 값을 덧붙일 때마다, 세 위치의 최솟값과 최댓값이 이루는 구간에서 a의 최댓값을 모든 삼중항에 대해 더한 값을 구한다.
문제
Today, Esmaan decided to prove to the world that he is not ordinary. He went to take the exam to become a data structures master. But the first question of the exam stumped him. Help him solve the problem:
You have a sequence of integers . In addition, you have three empty sequences: , , and .
- Let be the maximum among the numbers .
- Let be .
- Let be the sum of the values for all possible combinations where , , and .
You need to perform queries of the following type:
- " ": add the value to the end of sequence .
After each query, output modulo .
입력
The first line contains two integers and (): the number of elements in the sequence and the number of queries.
The second line contains integers (): the elements of the sequence.
Then follow lines, each containing a query in the format " " (X \in \\{A, B, C\\}, ).
출력
After each query, output a line with a single integer: the current value of modulo .