| 시간 제한 | 메모리 제한 | 제출 | 정답 | 맞힌 사람 | 정답 비율 |
|---|---|---|---|---|---|
| 3 초 | 2048 MB | 5 | 3 | 2 | 100.000% |
Once upon a time, there was a magician who had studied computer science for several years. One day, he was deeply impressed by the radix sort since it felt like real magic to him. He decided to use a similar mechanism to invent a new card shuffling magic trick. However, he had to make it more sophisticated so that no one would notice anything until the very end of the performance. Here’s the trick he wrote down in his notebook:
| Assistant | ADA | BOB | JOHN | MAX | ZOE |
|---|---|---|---|---|---|
| Work order | 5ドル$ | 2ドル$ | 3ドル$ | 1ドル$ | 4ドル$ |
| Receiver of Group-0ドル$ cards | JOHN | MAX | JOHN | BOB | JOHN |
| Receiver of Group-1ドル$ cards | ADA | ZOE | ZOE | ZOE | ZOE |
Since the audience may choose any $K$ and write any length-$K$ binary numbers without control, the magician carefully designed the distribution table and the work order so that the binary numbers can always be sorted in non-decreasing lexicographical order at the end of the above procedure regardless of the initial configuration.
The magician had a rehearsal with the distribution table in Table 1. At the beginning $K = 3$ was chosen each assistant received the cards as described in the first two rows in Table 2; note that the assistants are written in their work order, in which they perform Step-3b described above. The third and fourth rows of Table 2 show how the cards were divided at the first round. For example, MAX divided the cards into Group-0ドル$ $(010, 100)$ and Group-1ドル$ $(111, 011),ドル after which he put Group-0ドル$ cards in front of BOB, and Group-1ドル$ cards in front of ZOE.
| Assistant | MAX | BOB | JOHN | ZOE | ADA |
|---|---|---|---|---|---|
| Cards (initial) | 010,ドル 111, 011, 100$ | 001,ドル 000$ | - | 000ドル$ | 110,ドル 111$ |
| Group-0ドル$ cards | 010,ドル 100$ | 000ドル$ | - | 000ドル$ | 110ドル$ |
| Group-1ドル$ cards | 111,ドル 011$ | 001ドル$ | - | - | 111ドル$ |
| Cards after Round 1ドル$ | 000ドル$ | 010,ドル 100$ | 000,ドル 110$ | 111,ドル 011, 001$ | 111ドル$ |
The last row of Table 2 shows the stacked cards in front of each assistant at the end of the first round. For example, ZOE received three cards $(111, 011, 001)$: two cards $(111, 011)$ from MAX, and the other card $(001)$ from BOB. And they were collected in the work order of the assistants who gave the cards.
The other two rounds were performed similarly, and the following table shows the cards stacked in front of each assistant at the end of the two rounds. Notice that after Round 3ドル,ドル one can obtain the binary numbers in non-decreasing lexicographical order by collecting the cards in work order of the assistants.
| Assistant | MAX | BOB | JOHN | ZOE | ADA |
|---|---|---|---|---|---|
| Cards after Round 2ドル$ | 100ドル$ | 000ドル$ | 000,ドル 001$ | 010,ドル 110, 111, 011$ | 111ドル$ |
| Cards after Round 3ドル$ | 000ドル$ | - | 000,ドル 001, 010, 011$ | 100,ドル 110, 111$ | 111ドル$ |
Once day, the magician noticed that there might exist more than one work orders for the same distribution table with which the above procedure always ends up with the binary numbers sorted correctly. For example, the procedure still works well even if BOB and MAX are swapped in the work order. The magician wants to know how many such work orders exist for his distribution table.
Given a distribution table, write a program to find the number of work orders with which the procedure works correctly in every case, regardless of the value of $K$ and the binary numbers written on the cards. It is guaranteed that at least one such work order exists for the distribution table.
Your program is to read from standard input. The input starts with a line containing an integer $n$ (2ドル ≤ n ≤10^5$), where $n$ is the number of assistants. Then the distribution table is given in the following $n$ lines. Each line contains the names of three assistants $X,ドル $Y,ドル $Z,ドル meaning that at each round $k,ドル $X$ gives the cards with 0ドル$-bit at the $k$-th least significant bit to $Y,ドル and those with 1ドル$-bit to $Z$. Each name is a non-empty string consisting of up to four English upper letters. Every assistant appears as a receiver at least once in the table.
Your program is to write to standard output. Print exactly one number $W$ modulo 101ドル,287円$ where $W ≥ 1$ is the number of work orders with which it is always possible to sort equal-length binary numbers in non- decreasing lexicographical order using the described procedure with the given distribution table.
5 ADA JOHN ADA BOB MAX ZOE JOHN JOHN ZOE MAX BOB ZOE ZOE JOHN ZOE
2
2 ADA BOB ADA BOB BOB ADA
1
8 A A E B A G C A F D A H E A H F C H G B H H D H
4