#P14812. [CCPC 2024 哈尔滨站] 树上游戏

[CCPC 2024 哈尔滨站] 树上游戏

Description

There is a tree (a connected undirected graph with nn nodes and n1n-1 edges) consisting of nn nodes, with nodes numbered from 11 to nn. Clearly, there is a unique simple path between any two nodes in the tree.

Xiaohong and Xiaolan are playing a game on this tree. In each game, both players independently and uniformly\textbf{independently and uniformly} select a random simple path from all n(n1)2\frac{n(n-1)}{2} simple paths (regardless of direction) that exist in the tree. Note that they may choose the same path. Let XX denote the number of edges that are common to both selected paths, and the score of the game is X2X^2.

Your task is to find the expected value of the score E(X2)E(X^2) when Xiaohong and Xiaolan play the game once, and output the result modulo 998244353998244353 (see the output format for details).

Input Format

The first line contains a positive integer TT (1T1041\le T \le 10^4), representing the number of test cases.

For each test case, the first line contains a positive integer nn (2n1052\le n \le 10^5), representing the number of nodes in the tree.

The next n1n-1 lines each contain two positive integers u,vu, v (1u,vn1\le u,v \le n), indicating that there is an edge between nodes uu and vv. The input is guaranteed to be a tree.

The sum of all nn over all test cases does not exceed 10610^6.

Output Format

For each test case, output a single integer, representing the answer modulo 998244353998244353.

Formally, let M=998244353M=998244353. It can be shown that the answer can be expressed as an irreducible fraction pq\frac p q, where pp and qq are integers and q≢0(modM)q \not\equiv 0\pmod M. Output the integer equal to pq1(modM)p\cdot q^{-1}\pmod M, where q1q^{-1} denotes the modular multiplicative inverse of qq modulo MM. In other words, output such an integer xx that 0x<M0\le x < M and xqp(modM)x\cdot q\equiv p\pmod M. It can be proved that there is exactly one xx which meets the condition.

2
3
1 2
2 3
5
1 2
1 5
3 2
4 2
443664158
918384806

Hint

For the first test case in the example, the answer without taking the modulo is 109\frac{10}{9}.

Among the 99 possible cases:

  • In 22 cases, the number of common edges between the two paths is 00;
  • In 66 cases, the number of common edges between the two paths is 11;
  • In 11 case, the number of common edges between the two paths is 22.

Therefore, the answer is $E(X^2) = \frac{2 \cdot 0^2 + 6 \cdot 1^2 + 1 \cdot 2^2}{9} = \frac{10}{9}$.