创建于 2026/07/18 · 更新于 2026/07/18
You start with an empty stack and push the elements in this exact increasing order, each exactly once. Between the pushes, you may interleave any number of pop operations, each removing the current top of the stack.
A sequence of operations is complete if, in the end, every element has been pushed and the stack is empty (so it contains exactly pushes and pops, and at no moment have there been more pops than pushes).
Each element has a patience : after element is pushed, at least more push operations must occur before is allowed to be popped. Since the elements are pushed in increasing order, this is the same as saying that element may be popped only after element has been pushed (that is, once all of are in). In particular, if , there are not enough later elements, so element can never be popped.
Count the complete operation sequences that satisfy the patience constraint of every element, modulo .
The first line contains a single integer .
The second line contains integers .
, .
Print a single integer — the number of valid complete operation sequences, modulo .
Note that if some element satisfies , then fewer than pushes remain after it, so it can never be popped in time, and the answer is .
3
0 0 0
5
4
2 0 0 0
7
2
2 0
0