#x1011. cf1630E Expected Components

cf1630E Expected Components

Expected Components

题面翻译

一个长度为 n n 的环状序列 {ai} \{a_i\} ,其中的数值满足 1ain 1\leq a_i\leq n ,序列中可能有相等的数。

序列 {ai} \{a_i\} 的一个排列和另外一个排列本质相同,当且仅当可以通过旋转使它们变得每一项都对应相等。

对于 {ai} \{a_i\} 的任何一种本质不同排列,可以构造一张 n n 个点的无向图,节点 i i 对应序列中的 ai a_i 。若序列中相邻两项相等(首项和末项也相邻),则在它们的对应的节点之间连一条边。这个排列的价值为无向图的连通块数量。

给定序列 {ai} \{a_i\} ,从它的所有本质不同排列中等概率选择一个,求价值的期望。

题目描述

Given a cyclic array a a of size n n , where ai a_i is the value of a a in the i i -th position, there may be repeated values. Let us define that a permutation of a a is equal to another permutation of a a if and only if their values are the same for each position i i or we can transform them to each other by performing some cyclic rotation. Let us define for a cyclic array b b its number of components as the number of connected components in a graph, where the vertices are the positions of b b and we add an edge between each pair of adjacent positions of b b with equal values (note that in a cyclic array the first and last position are also adjacents).

Find the expected value of components of a permutation of a a if we select it equiprobably over the set of all the different permutations of a a .

输入格式

The input consists of multiple test cases. The first line contains a single integer t t ( 1t105 1 \le t \le 10^5 ) — the number of test cases. Description of the test cases follows.

The first line of each test case contains a single integer n n ( 1n106 1 \le n \le 10^6 ) — the size of the cyclic array a a .

The second line of each test case contains n n integers, the i i -th of them is the value ai a_i ( 1ain 1 \le a_i \le n ).

It is guaranteed that the sum of n n over all test cases does not exceed 106 10^6 .

It is guaranteed that the total number of different permutations of a a is not divisible by M M

输出格式

For each test case print a single integer — the expected value of components of a permutation of a a if we select it equiprobably over the set of all the different permutations of a a modulo 998244353 998\,244\,353 .

Formally, let M=998244353 M = 998\,244\,353 . It can be shown that the answer can be expressed as an irreducible fraction pq \frac{p}{q} , where p p and q q are integers and q≢0(modM) q \not \equiv 0 \pmod{M} . Output the integer equal to pq1modM p \cdot q^{-1} \bmod M . In other words, output such an integer x x that 0x<M 0 \le x < M and xqp(modM) x \cdot q \equiv p \pmod{M} .

样例 #1

样例输入 #1

5
4
1 1 1 1
4
1 1 2 1
4
1 2 1 2
5
4 3 2 5 1
12
1 3 2 3 2 1 3 3 1 3 3 2

样例输出 #1

1
2
3
5
358642921

提示

In the first test case there is only 1 1 different permutation of a a :

  • [1,1,1,1] [1, 1, 1, 1] has 1 1 component.
  • Therefore the expected value of components is 11=1 \frac{1}{1} = 1

In the second test case there are 4 4 ways to permute the cyclic array a a , but there is only 1 1 different permutation of a a :

  • [1,1,1,2] [1, 1, 1, 2] , [1,1,2,1] [1, 1, 2, 1] , [1,2,1,1] [1, 2, 1, 1] and [2,1,1,1] [2, 1, 1, 1] are the same permutation and have 2 2 components.
  • Therefore the expected value of components is 21=2 \frac{2}{1} = 2

In the third test case there are 6 6 ways to permute the cyclic array a a , but there are only 2 2 different permutations of a a :

  • [1,1,2,2] [1, 1, 2, 2] , [2,1,1,2] [2, 1, 1, 2] , [2,2,1,1] [2, 2, 1, 1] and [1,2,2,1] [1, 2, 2, 1] are the same permutation and have 2 2 components.
  • [1,2,1,2] [1, 2, 1, 2] and [2,1,2,1] [2, 1, 2, 1] are the same permutation and have 4 4 components.
  • Therefore the expected value of components is 2+42=62=3 \frac{2+4}{2} = \frac{6}{2} = 3

In the fourth test case there are 120 120 ways to permute the cyclic array a a , but there are only 24 24 different permutations of a a :

  • Any permutation of a a has 5 5 components.
  • Therefore the expected value of components is 24524=12024=5 \frac{24\cdot 5}{24} = \frac{120}{24} = 5