loj#P3042. 「ZJOI2019」麻将

「ZJOI2019」麻将

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(n5)n(n \ge 5) ranks of tiles (ranked from 11 to nn), 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 ii, i+1i+1, i+2i+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, 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 these are not:

  • {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\}

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

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

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

Input

The first line contains a single number nn.
Each of the next 1313 lines contains 2 numbers w,tw, t — the ii-th tile Karen draws is the tt-th tile of rank ww.

Output

Print a single number — the answer mod998244353\bmod 998244353, i.e. if the answer's simplest fraction is xy\frac{x}{y} (x0,(x \ge 0, y1,y \ge 1, gcd(x,y)=1)\gcd(x, y) = 1), you should print x×y1mod998244353x \times y^{−1} \bmod 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

Limits

Cases 1 and 2 satisfy n=5n = 5.
Cases 3-5 satisfy n13n \le 13.
Cases 6 and 7 satisfy n100,wi=i,ti=1n \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 5n1005 \le n \le 100.