| Codeforces Round 1121 (Div. 2) |
|---|
| Finished |
Madam Madamant is expecting a baby and already planning a skating dynasty in which every parent is a stronger skater than their children.
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.
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$$$.
The cost of a valid dynasty is $$$$$$ \sum_{v \ne r} (a_{p_v} - a_v). $$$$$$
Two dynasties are different if their roots are different or if the parent of at least one skater is different.
Find the sum of the costs of all valid dynasties Madamant can form, modulo $$$998\,244\,353$$$.
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.
The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of skaters.
The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — their ratings.
It is guaranteed that all $$$a_i$$$ are pairwise distinct.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
For each test case, print one integer — the sum of the costs of all valid dynasties, modulo $$$998\,244\,353$$$.
511031 2 344 1 3 221 100000000052 7 1 10 4
05271755646414
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$$$.
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$$$.
| Name |
|---|


