class Bit: """1-indexed""" def __init__(self, n): self.size = n self.tree = [0] * (n + 1) def _sum(self, i): s = 0 while i > 0: s += self.tree[i] i -= i & -i s %= MOD return s def sum(self, i, j): """閉区間[i, j]""" return (self._sum(j) - self._sum(i - 1)) % MOD def add(self, i, x): while i <= self.size: self.tree[i] += x i += i & -i N = int(input()) A = tuple(map(int, input().split())) As = sorted(list(set(A))) comp_d = {x: i for i, x in enumerate(As, 1)} MOD = 998244353 bleft, bleft_ct = Bit(N), Bit(N) table_left, ct_left = [0] * N, [0] * N for i, a in enumerate(A): c = comp_d[a] bleft.add(c, a) table_left[i] = bleft.sum(c + 1, N) bleft_ct.add(c, 1) ct_left[i] = bleft_ct.sum(c + 1, N) bright, bright_ct = Bit(N), Bit(N) table_right, ct_right = [0] * N, [0] * N for i, a in enumerate(reversed(A)): c = comp_d[a] bright.add(c, a) table_right[N - 1 - i] = bright.sum(1, c - 1) bright_ct.add(c, 1) ct_right[N - 1 - i] = bright_ct.sum(1, c - 1) ans = 0 for a, l, lc, r, rc in zip(A, table_left, ct_left, table_right, ct_right): ans += l * rc + a * lc * rc + r * lc ans %= MOD print(ans)