Math
ๆฐๅญฆ
ABC
E
ABC221 E - LEQ
https://atcoder.jp/contests/abc221/tasks/abc221_e (opens in a new tab)
ๆฐด่ฒไธไฝใๆฐๅญฆใ
้กๆใฏ ใฎ ใๆบใใ้จๅๅ ใฎ็ทๆฐใๆฑใใใ
้จๅๅใฎไธก็ซฏใฎใคใณใใใฏใน ใๆฑบใพใใฐใ้ใฎ ๅใฎ่ฆ็ด ใซ้ขใใฆใฏๅใ๏ผๅใใชใใฎ 2 ้ใใฎ้ธๆใซใใ้จๅๅใๆงๆใใใใใใใใฎๅ ดๅใฎ้จๅๅใฎ็ทๆฐใฏ ๅใจใชใใ
ใคใพใ ใจใชใ ใซๅฏพใใฆใ ใฎ็ทๅใๆฑใใใฐใใใใจใจใชใใ
ไปใ ใฏ Binary Indexed Tree ใซใใ้ซ้ใซ่จ็ฎใใงใใใฎใงใ ใซใคใใฆๅ จๆข็ดขใใใฐ่งฃใๅพใใใใ
from atcoder.fenwicktree import FenwickTree
MOD = 998244353
n = int(input())
a = list(map(int, input().split()))
compressed = {e: i for i, e in enumerate(sorted(set(a)))}
bit = FenwickTree(n)
remain = []
ans = 0
for i in reversed(range(n)):
e = compressed[a[i]]
ans += bit.sum(e, n) * pow(pow(2, MOD - 2, MOD), i, MOD)
ans %= MOD
if i > 0:
bit.add(e, pow(2, i - 1, MOD))
else:
bit.add(e, pow(2, MOD - 2, MOD))
print(ans)
้ๅ ใฎ่จ็ฎใซๆ้ๅใฃใใ