Advertisement
Guest User

e.py

a guest
Jan 2nd, 2025
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.97 KB | None | 0 0
  1. from sys import stdin
  2. def input(): return stdin.readline().strip('\n')
  3.  
  4. class PrefixSum:
  5.     def __init__(self, arr) -> None:
  6.         self.prefix = [0] * (len(arr))
  7.         self.prefix[0] = arr[0]
  8.         for i in range(1, len(arr)):
  9.             self.prefix[i] = self.prefix[i - 1] + arr[i]
  10.    
  11.     # 0 indexed from l to r (inclusive)
  12.     def sum(self, l, r):
  13.         if l == 0:
  14.             return self.prefix[r]
  15.         return self.prefix[r] - self.prefix[l - 1]
  16.  
  17. MOD = 998244353
  18. def func(n, x):
  19.     num = x * (n + 2) - n
  20.     denom = x + 1
  21.     num %= MOD
  22.     return (num * pow(denom, -1, MOD)) % MOD
  23.  
  24. def solve():
  25.     n = int(input())
  26.     arr = list(map(int, input().split()))
  27.  
  28.     heights = [0] * (n + 1)
  29.     for i in arr:
  30.         heights[i] += 1
  31.     pf = PrefixSum(heights)
  32.     ans = 0
  33.  
  34.     for h in range(1, n + 1):
  35.         ans += func(n, pf.sum(h, n)) - h * heights[h]
  36.         ans %= MOD
  37.     print(ans)
  38.  
  39. for _ in range(int(input())):
  40.     solve()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement