3042 [ZJOI 2019] Mahjong

内存限制:512 MB 时间限制:2000 ms

Description

The translation of riichi terms comes from WRC and EMA

Kujo Karen loves playing Majsoul, so she makes out a problem about Majsoul. Wish you won't no longer love Majsoul because of this problem.

1.png

Today Karen decides to play Majsoul, but all her friends have gone to play Dota AutoChess and Karen have to play mahjong alone. She finds a special mahjong set. The mahjong set consists of $n(n \ge 5)$ ranks of tiles (ranked from $1$ to $n$), and each rank of tiles include 4 tiles (numbered from 1 to 4). In this problem, we DO NOT consider their suits.

A chow (shuntsu 順子) is three consecutive tiles (i.e. their ranks are $i$, $i+1$, $i+2$ respectively). A pung (kōtsu 刻子) is composed of three identical tiles. A group (mentsu 面子) is either a chow or pung (In this problem, we DO NOT consider kong). A pair (toitsu 対子) is composed of two identical tiles.

A player's hand (his/her 14 tiles) is a winning hand if it has four groups and a pair, or seven different pairs.

For example, these hands are winning hands: - ${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, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7}$

And these are not: - ${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, 2, 3, 4, 5, 6, 7, 8, 9, 9, 9, 11}$

Firstly, Karen draws $13$ tiles from the wall, and shuffle the remaining $4n-13$ tiles. The shuffle is at random, i.e. all the $(4n-13)!$ permutations have equal probability to appear.

For a permutation $P$, denote $S_i$ as a mahjong hand composed of Karen's $13$ prepared tiles and $P$'s first $i$ tiles. the weight of $P$ is the smallest $i$ which can let $S_i$ exist a subset that is a winning hand. If you are familiar with mahjong, obviously the weight of $P$ equals to the minimum turns to complete a winning hand. Notice that when $n \ge 5$, $S_{4n-13}$ always has a subset that is a winning hand, so the weight of $P$ is well-defined.

She lets you to calculate the expected value of the weight of $P$ in advance.

Input

The first line contains a single number $n$.
Each of the next $13$ lines contains 2 numbers $w, t$ — the $i$-th tile Karen draws is the $t$-th tile of rank $w$.

Output

Print a single number — the answer $\bmod 998244353$, i.e. if the answer's simplest fraction is $\frac{x}{y}$ $(x \ge 0,$ $y \ge 1,$ $\gcd(x, y) = 1)$, you should print $x \times y^{−1} \bmod 998244353$.

Sample

It forms pure nine gates. Obviously, no matter what card is added, it would complete a winning hand. Therefore, for every $P$, its weight is $1$, and that's why the expected value of the weight of $P$ is $1$.

Limits

Cases 1 and 2 satisfy $n = 5$.
Cases 3-5 satisfy $n \le 13$.
Cases 6 and 7 satisfy $n \le 100, w_i = i, t_i = 1$.
Cases 8 and 9 satisfy $n \le 100, w_i = \large\lceil\normalsize \frac{i}{4} \large\rceil\normalsize, t_i = i \bmod 4 + 1$.
All test cases satisfy $5 \le n \le 100$.

Samples

Input 1

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

Output 1

1