atcoder#AGC017F. [AGC017F] Zigzag
[AGC017F] Zigzag
Score : points
Problem Statement
There are dots arranged to form an equilateral triangle whose sides consist of dots, as shown below. The -th dot from the left in the -th row from the top is denoted by (, ). Also, we will call immediately lower-left to , and immediately lower-right to .
Takahashi is drawing polygonal lines by connecting these dots. Each starts at , and visits the dot that is immediately lower-left or lower-right to the current dots times. More formally, there exist such that:
- connects the points , in this order.
- For each , either or holds.
Takahashi would like to draw these lines so that no part of is to the left of . That is, for each , must hold.
Additionally, there are conditions on the shape of the lines that must be followed. The -th condition is denoted by , which means:
- If , must visit the immediately lower-left dot for the -th move.
- If , must visit the immediately lower-right dot for the -th move.
That is, must hold.
In how many ways can Takahashi draw polygonal lines? Find the count modulo .
Notes
Before submission, it is strongly recommended to measure the execution time of your code using "Custom Test".
Constraints
- or
- No pair appears more than once as .
Input
Input is given from Standard Input in the following format:
:
Output
Print the number of ways for Takahashi to draw polygonal lines, modulo .
3 2 1
1 2 0
6
There are six ways to draw lines, as shown below. Here, red lines represent , and green lines represent .
3 2 2
1 1 1
2 1 0
0
5 4 2
1 3 1
4 2 0
172
20 20 0
881396682