atcoder#ARC127E. [ARC127E] Priority Queue
[ARC127E] Priority Queue
Score : points
Problem Statement
Given is a sequence of integers , which contains exactly ones and exactly twos.
Snuke has a set , which is initially empty. He is going to do operations. The -th operation is as follows.
- If : Choose an integer such that and add it to . Here, must not be an integer that was chosen in a previous operation.
- If : Delete from the element with the largest value. The input guarantees that is not empty just before this operation.
How many sets are there that can be the final state of ? Find the count modulo .
Constraints
- for exactly indices .
- for exactly indices .
- will not be empty just before an operation with .
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print the answer modulo .
3 1
1 1 2 1
2
There are two possible final states of : .
For example, the following sequence of operations results in .
- : Add to .
- : Add to .
- : Delete from .
- : Add to .
20 6
1 1 1 1 1 2 1 1 1 2 1 2 1 2 1 2 1 1 1 1 2 1 1 1 1 1
5507