第 1121 场 Codeforces Round(Div. 2)
C
马达曼特的滑冰王朝
Madamant's Skating Dynasty
Codeforces
1400
原题
← BD →

马达曼特的滑冰王朝AI (CI翻译)

Madam Madamant is expecting a baby and already planning a skating dynasty in which every parent is a stronger skater than their children.

马达曼特女士即将迎来一个宝宝,她已经开始筹划建立一个滑冰王朝,要求其中每位家长的滑冰实力都强于自己的孩子。AI (CI翻译)

Formally, Madamant has $n$ labeled skaters. Skater $v$ has an integer rating $a_v$, and all ratings are pairwise distinct. A possible dynasty is represented by a rooted tree on these skaters.

形式化地,马达曼特有 $n$ 名带编号的滑冰选手。选手 $v$ 的实力值为整数 $a_v$,所有选手的实力值两两不同。一个可能的王朝由一棵以这些选手为顶点的有根树表示。AI (CI翻译)

Let $r$ be the root of the tree. For every skater $v \ne r$, let $p_v$ be the parent of $v$. The dynasty is valid if $a_v \lt a_{p_v}$ for every $v \ne r$.

设 $r$ 为树根。对于每位选手 $v \ne r$,设 $p_v$ 为 $v$ 的父节点。如果对于所有 $v \ne r$ 都有 $a_v \lt a_{p_v}$,则称这个王朝是合法的。AI (CI翻译)

The cost of a valid dynasty is $$ \sum_{v \ne r} (a_{p_v} - a_v). $$

一个合法王朝的代价为 $$ \sum_{v \ne r} (a_{p_v} - a_v). $$AI (CI翻译)

Two dynasties are different if their roots are different or if the parent of at least one skater is different.

如果两个王朝的树根不同,或者至少有一位选手的父节点不同,则认为这两个王朝不同。AI (CI翻译)

Find the sum of the costs of all valid dynasties Madamant can form, modulo $998\,244\,353$.

求马达曼特能够组成的所有合法王朝的代价之和,对 $998\,244\,353$ 取模。AI (CI翻译)

Input

输入格式

AI (CI翻译)

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

每个测试包含多组测试用例。第一行包含测试用例的数量 $t$($1 \le t \le 10^4$)。接下来是各组测试用例的描述。AI (CI翻译)

The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of skaters.

每组测试用例的第一行包含一个整数 $n$($1 \le n \le 2 \cdot 10^5$)——滑冰选手的数量。AI (CI翻译)

The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) — their ratings.

第二行包含 $n$ 个整数 $a_1, a_2, \ldots, a_n$($1 \le a_i \le 10^9$)——各位选手的实力值。AI (CI翻译)

It is guaranteed that all $a_i$ are pairwise distinct.

保证所有 $a_i$ 两两不同。AI (CI翻译)

It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.

保证所有测试用例的 $n$ 之和不超过 $2 \cdot 10^5$。AI (CI翻译)

Output

输出格式

AI (CI翻译)

For each test case, print one integer — the sum of the costs of all valid dynasties, modulo $998\,244\,353$.

对于每组测试用例,输出一个整数——所有合法王朝的代价之和,对 $998\,244\,353$ 取模。AI (CI翻译)

Note

说明

AI (CI翻译)

In the second test case, the skater with rating $3$ must be the root. The parent of the skater with rating $2$ must be the skater with rating $3$, while the skater with rating $1$ can choose either of the other skaters as their parent. The two valid dynasties have costs $2$ and $3$, so the answer is $5$.

在第二组测试用例中,实力值为 $3$ 的选手必须是树根。实力值为 $2$ 的选手的父节点必须是实力值为 $3$ 的选手,而实力值为 $1$ 的选手可以选择另外两位选手中的任意一位作为父节点。这两个合法王朝的代价分别为 $2$ 和 $3$,因此答案为 $5$。AI (CI翻译)

In the fourth test case, there is only one valid dynasty. Its cost is $10^9 - 1 = 999\,999\,999$, whose remainder modulo $998\,244\,353$ is $1\,755\,646$.

在第四组测试用例中,只有一个合法王朝。它的代价为 $10^9 - 1 = 999\,999\,999$,对 $998\,244\,353$ 取模后的余数为 $1\,755\,646$。AI (CI翻译)

样例

Input #1
5
1
10
3
1 2 3
4
4 1 3 2
2
1 1000000000
5
2 7 1 10 4
Output #1
0
5
27
1755646
414
InputCopy OutputCopy
题目信息
题号
2264C
时间限制
2 s
内存限制
256 MB
标签
组合数学
数学
排序

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。