#agc041f. [agc041_f]Histogram Rooks
[agc041_f]Histogram Rooks
Problem Statement
Let us consider a grid of squares with rows and columns. Arbok has cut out some part of the grid so that, for each , the bottommost squares are remaining in the -th column from the left. Now, he wants to place rooks into some of the remaining squares.
A rook is a chess piece that occupies one square and can move horizontally or vertically, through any number of unoccupied squares. A rook can not move through squares that have been cut out by Arbok.
Let's say that a square is covered if it either contains a rook, or a rook can be moved to this square in one move.
Find the number of ways to place rooks into some of the remaining squares so that every remaining square is covered, modulo .
Constraints
- All input values are integers.
Input
Input is given from Standard Input in the following format:
Output
Print the number of ways to place rooks into some of the remaining squares so that every remaining square is covered, modulo .
Sample Input 1
2
2 2
Sample Output 1
11
Any placement with at least two rooks is fine. There are such placements.
Sample Input 2
3
2 1 2
Sample Output 2
17
The following rook placements satisfy the conditions (R
denotes a rook, *
denotes an empty square):
R * * R * * R R R R R R
**R R** R*R R** *R* **R
R * R * * R * R * * R R
R*R *RR RR* R*R RRR RR*
R R R R R * * R R R
R*R *RR RRR RRR RRR
Sample Input 3
4
1 2 4 1
Sample Output 3
201
Sample Input 4
10
4 7 4 8 4 6 8 2 3 6
Sample Output 4
263244071