#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 n(n5)n(n\ge 5) different tile values, with values from 11 to nn. For each value, there are 44 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 i,i,i(1in)i,i,i(1 \le i \le n) or i,i+1,i+2(1in2)i,i+1,i+2(1\le i\le n-2). A pair is defined as two tiles of the same value, i.e., with values of the form i,i(1in)i,i(1 \le i \le n).

A multiset of Mahjong tiles SS is said to be winning if and only if its size is 1414 and it satisfies at least one of the following two conditions:

  • SS can be partitioned into five multisets S1S_1 to S5S_5, where S1S_1 is a pair, and S2S_2 to S5S_5 are melds.
  • SS can be partitioned into seven multisets S1S_1 to S7S_7, 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):

  • {1,1,1,1,2,3,4,5,6,7,8,9,9,9}\{1,1,1,1,2,3,4,5,6,7,8,9,9,9\}
  • {1,1,2,2,4,4,5,5,6,6,7,7,8,8}\{1,1,2,2,4,4,5,5,6,6,7,7,8,8\}
  • {1,1,2,2,3,3,4,4,5,5,6,6,7,7}\{1,1,2,2,3,3,4,4,5,5,6,6,7,7\}

And the following multisets are all not winning:

  • {1,1,1,2,3,4,5,6,7,8,9,9,9}\{1,1,1,2,3,4,5,6,7,8,9,9,9\}
  • {1,1,1,1,4,4,5,5,6,6,7,7,8,8}\{1,1,1,1,4,4,5,5,6,6,7,7,8,8\}
  • {1,1,1,2,3,4,5,6,7,8,9,9,9,11}\{1,1,1,2,3,4,5,6,7,8,9,9,9,11\}

Kelian first draws 1313 tiles, and then randomly shuffles the remaining 4n134n-13 tiles. The shuffling is uniformly random, i.e., all (4n13)!(4n-13)! permutations occur with equal probability.

For a permutation PP, Kelian defines SiS_i as the multiset formed by the 1313 tiles she drew in advance plus the first ii tiles in PP. The weight of PP is defined as the minimum ii such that SiS_i has a subset that is winning. If you are familiar with Mahjong, it is not hard to see that the weight of PP is the earliest theoretical turn to win. Note that when n5n\ge 5, S4n13S_{4n-13} always has a winning subset, so the weight of PP 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 PP.

Input Format

The first line contains an integer nn, indicating the number of different tile values in this special Mahjong set.

Then follow 1313 lines, each containing two integers w,t(1wn,1t4)w,t(1 \le w \le n,1 \le t \le 4), indicating that the ii-th tile Kelian initially drew is the tt-th tile of value ww. It is guaranteed that the pairs (w,t)(w,t) are pairwise distinct.

Output Format

Output one line containing one integer, indicating the answer modulo 998244353998244353. That is, if the answer in lowest terms is xy(x0,y1,gcd(x,y)=1)\frac{x}{y}(x \ge 0,y \ge 1,gcd(x,y) = 1), you need to output x×y1 mod998244353x\times y^{-1}\ \mathrm{mod}\: 998244353.

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 PP, the weight is 11, so the expected weight is 11.

For 20%20\% of the testdata, n=5n = 5.

For 50%50\% of the testdata, n13n\le 13.

For another 20%20\% of the testdata, n100,wi=i,ti=1n \le 100,w_i = i,t_i = 1.

For another 20%20\% of the testdata, $n \le 100,w_i = \lceil \frac{i}{4} \rceil ,t_i= i\ \mathrm{mod}\ 4 + 1$.

For 100%100\% of the testdata, 5n1005 \le n \le 100.

Translated by ChatGPT 5