#P5279. [ZJOI2019] 麻将
[ZJOI2019] 麻将
Description
Jiutiao Kelian is a girl who loves playing Mahjong. So she made a Mahjong-related problem, hoping this problem will not make your love for Mahjong disappear completely.

Today, Kelian wants to play Mahjong, but all her friends went to play Auto Chess, so Kelian can only play alone. Kelian found a special Mahjong set. It has different tile values, with values from to . For each value, there are tiles.
A meld is defined as three tiles that are either all the same value or consecutive values, i.e., with values of the form or . A pair is defined as two tiles of the same value, i.e., with values of the form .
A multiset of Mahjong tiles is said to be winning if and only if its size is and it satisfies at least one of the following two conditions:
- can be partitioned into five multisets to , where is a pair, and to are melds.
- can be partitioned into seven multisets to , all of which are pairs, and their corresponding values are pairwise distinct.
For example, the following multisets are all winning (only the values are shown here):
And the following multisets are all not winning:
Kelian first draws tiles, and then randomly shuffles the remaining tiles. The shuffling is uniformly random, i.e., all permutations occur with equal probability.
For a permutation , Kelian defines as the multiset formed by the tiles she drew in advance plus the first tiles in . The weight of is defined as the minimum such that has a subset that is winning. If you are familiar with Mahjong, it is not hard to see that the weight of is the earliest theoretical turn to win. Note that when , always has a winning subset, so the weight of is well-defined.
Now Kelian wants to train her tile efficiency, so she hopes you can first compute the expected value of the weight of .
Input Format
The first line contains an integer , indicating the number of different tile values in this special Mahjong set.
Then follow lines, each containing two integers , indicating that the -th tile Kelian initially drew is the -th tile of value . It is guaranteed that the pairs are pairwise distinct.
Output Format
Output one line containing one integer, indicating the answer modulo . That is, if the answer in lowest terms is , you need to output .
9
1 1
1 2
1 3
2 1
3 1
4 1
5 1
6 1
7 1
8 1
9 1
9 2
9 3
1
Hint
The above hand type is called Pure Nine Gates. It is not hard to see that no matter what tile you add, it is winning. Therefore, for all permutations , the weight is , so the expected weight is .
For of the testdata, .
For of the testdata, .
For another of the testdata, .
For another of the testdata, $n \le 100,w_i = \lceil \frac{i}{4} \rceil ,t_i= i\ \mathrm{mod}\ 4 + 1$.
For of the testdata, .
Translated by ChatGPT 5
京公网安备 11011102002149号