Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from sys import stdin
- def input(): return stdin.readline().strip('\n')
- class PrefixSum:
- def __init__(self, arr) -> None:
- self.prefix = [0] * (len(arr))
- self.prefix[0] = arr[0]
- for i in range(1, len(arr)):
- self.prefix[i] = self.prefix[i - 1] + arr[i]
- # 0 indexed from l to r (inclusive)
- def sum(self, l, r):
- if l == 0:
- return self.prefix[r]
- return self.prefix[r] - self.prefix[l - 1]
- MOD = 998244353
- def func(n, x):
- num = x * (n + 2) - n
- denom = x + 1
- num %= MOD
- return (num * pow(denom, -1, MOD)) % MOD
- def solve():
- n = int(input())
- arr = list(map(int, input().split()))
- heights = [0] * (n + 1)
- for i in arr:
- heights[i] += 1
- pf = PrefixSum(heights)
- ans = 0
- for h in range(1, n + 1):
- ans += func(n, pf.sum(h, n)) - h * heights[h]
- ans %= MOD
- print(ans)
- for _ in range(int(input())):
- solve()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement