atcoder#ABC217F. [ABC217F] Make Pair
[ABC217F] Make Pair
Score : points
Problem Statement
There are students standing in a row, numbered , , , from left to right. For all pairs of two students, they are on good or bad terms. Specifically, for each , Student and Student are on good terms; for the remaining pairs of two students, they are on bad terms.
The teacher is going to do the following operation times to form pairs of two students.
- Choose two adjacent students who are on good terms, pair them, and remove them from the row.
- If the removed students were not at an end of the row, close up the gap so that the two students who were to the left and right of the removed students are now adjacent.
Find the number, modulo , of possible ways to do the operation times. Two ways to do the operation times are considered different when there exists such that the pair of students chosen in the -th operation is different in those two ways.
Constraints
- All pairs are distinct.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print the number of possible ways to complete the procedure, modulo .
2 3
1 2
1 4
2 3
1
The only way to complete the procedure is to choose Students and in the first and Students and in the second. If Students and are chosen in the first operation, Students and will remain, who are on bad terms and thus cannot be paired in the second operation. Thus, you should print .
2 2
1 2
3 4
2
There are two ways to complete the procedure: one way is to choose Students and in the first operation and Students and in the second, and the other way is to choose Students and in the first operation and Students and in the second. Note that these two ways are distinguished.
2 2
1 3
2 4
0
Since no pair can be chosen in the first operation, there is no way to complete the procedure, so you should print .