atcoder#ARC118E. [ARC118E] Avoid Permutations
[ARC118E] Avoid Permutations
Score : points
Problem Statement
Let us consider the problem below for a permutation of , and denote the answer by .
We have an grid, where the rows are numbered from top to bottom and the columns are numbered from left to right. Let denote the square at the -th row and -th column.
Consider paths from to where we repeatedly move one square right or one square down. However, for each , we must not visit the square . How many such paths are there?
You are given a positive integer and an integer sequence , where each is or an integer between and (inclusive).
Consider all permutations of satisfying . Find the sum of over all such , modulo .
Constraints
- or .
- if and , for .
Input
Input is given from Standard Input in the following format:
Output
Print the answer.
4
1 -1 4 -1
41
We want to find the sum of over two permutations and . The figure below shows the impassable squares for each of them, where .
and #
represent a passable and impassable square, respectively.
...... ......
.#.... .#....
..#... ...#..
....#. ....#.
...#.. ..#...
...... ......
P=(1,2,4,3) P=(1,3,4,2)
- For , we have .
- For , we have .
The answer is the sum of them: .
4
4 3 2 1
2
In this sample, we want to compute for just one permutation .
3
-1 -1 -1
48
- For , we have .
- For , we have .
- For , we have .
- For , we have .
- For , we have .
- For , we have .
The answer is the sum of them: .