Under construction

Still being built. Things may change

East 2 Will Not Come to This Match! 9

Time limit5sMemory limit1024 MB

Summary
Count the ways to pick 14 of the N wall tiles, by position, that can form a dragon of one head and four bodies or seven distinct heads, modulo 998244353.
Level

Hard8 of 10

Topics
Combinatorics, Dynamic programming, Hash map
Solved
No attempts yet

Problem

This problem is identical to Problem 1 with the same title, except for the tile types and counts, the number of tiles in the wall, and the modulus of the answer.

Mahjong+ is a game where you collect 14 tiles to build a dragon. To build a dragon, you need to know about tiles, heads, and bodies.

  • Tile (牌): Mahjong+ has 100,000 copies of each tile, and each tile shows a number from 1 to 10 00010\,000 inclusive.
  • Head: A combination of 2 tiles with the same number (e.g. ).
  • Body: A combination of 3 tiles with consecutive numbers that differ by 1 (e.g. ), or 3 tiles with the same number (e.g. ). The order of the tiles does not matter.

The following table shows examples of what is and is not a head or body.

ExampleTypeExplanation
-The hand has one tile, so it is neither a head nor a body.
HeadThe two tiles match, so this combination is a head.
Body7, 8, and 9 are consecutive numbers, so this combination is a body.
-1, 3, and 5 are not consecutive numbers, so this combination is not a body.
-10410^4 and 11 are not consecutive numbers.
BodyThree tiles share the same number, so this combination is a body.
-4, 4, and 5 are not consecutive numbers, so this combination is not a body.
-The hand has four tiles, so it is neither a head nor a body.

To build a dragon, you need 14 tiles arranged either as 1 head and 4 bodies, or as 7 different heads.

The following table shows examples of dragons and non-dragons.

: Dragon
- A dragon with head (3,3)(3,3) and bodies (4,4,4);(5,6,7);(6,7,8);(7,8,9)(4,4,4) ;(5,6,7) ;(6,7,8) ;(7,8,9).
: Not a dragon
* No way to split these tiles gives 1 head and 4 bodies, or 7 heads.
  • The same tile cannot belong to two or more heads or bodies at the same time. | | : Dragon | | - A dragon with head (5,5)(5,5) and bodies (1,2,3);(1,2,3);(7,8,9);(7,8,9)(1,2,3) ;(1,2,3) ;(7,8,9) ;(7,8,9).
  • It can also be seen as a dragon with 7 different heads (1,1);(2,2);(3,3);(5,5);(7,7);(8,8);(9,9)(1,1) ;(2,2) ;(3,3) ;(5,5) ;(7,7) ;(8,8) ;(9,9). | | : Not a dragon | | * Making 7 heads requires two duplicate heads (1,1)(1,1), so it cannot have 7 heads.
  • No way to split these tiles gives 1 head and 4 bodies. |

The wall holds NN tiles, and the ii-th tile shows AiA_i. You want to know how many ways there are to draw 14 tiles from the wall and build a dragon.

Input

The first line contains the number of tiles NN in the wall. (14≤N≤100 000)(14\le N\le 100\,000)

The second line contains A1,A2,⋯ ,ANA_1,A_2,\cdots ,A_N, separated by spaces. (1≤Ai≤10 000)(1\le A_i\le 10\,000)

Output

Print the number of ways to draw 14 tiles from the wall and build a dragon, modulo 998 244 353998\,244\,353 (=119×223+1)(=119 \times 2^{23}+1). 998 244 353998\,244\,353 is prime.

Hints

  • Two draws with the same tile numbers still count as different cases if the tiles came from different positions.
  • If 14 tiles can form a dragon in several ways, it still counts as one case.

Examples3

  1. Example 1

    Input
    14
    1 1 2 2 3 3 5 5 7 7 8 8 9 9
    
    Expected output
    1
    
  2. Example 2

    Input
    36
    4 1 6 1 5 3 9 5 9 2 9 9 7 4 5 3 3 2 1 7 8 4 5 7 8 6 3 1 2 8 6 7 2 4 8 6
    
    Expected output
    445632532
    
  3. Example 3

    Input
    36
    1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
    
    Expected output
    801564141