MOD = 998244353 class Bit: def __init__(self, n): self.size = n self.n0 = 1 << (n.bit_length() - 1) self.tree = [0] * (n + 1) def range_sum(self, l, r): return self.sum(r - 1) - self.sum(l - 1) def sum(self, i): i += 1 s = 0 while i > 0: s += self.tree[i] s %= MOD i -= i & -i return s def get(self, i): return self.sum(i) - self.sum(i - 1) def add(self, i, x): i += 1 while i <= self.size: self.tree[i] += x self.tree[i] %= MOD i += i & -i def lower_bound(self, x): pos = 0 plus = self.n0 while plus > 0: if pos + plus <= self.size and self.tree[pos + plus] < x: x -= self.tree[pos + plus] pos += plus plus //= 2 return pos n = int(input()) A = list(map(int, input().split())) se = set(A) lst = sorted(se) dic = {a:i for i, a in enumerate(lst)} le = len(lst) bit1 = Bit(le) bit2 = Bit(le) cnt1 = Bit(le) cnt2 = Bit(le) ans = 0 for a in A: b = dic[a] ans += bit2.range_sum(b + 1, le) + a * cnt2.range_sum(b + 1, le) ans %= MOD bit2.add(b, bit1.range_sum(b + 1, le) + a * cnt1.range_sum(b + 1, le)) cnt2.add(b, cnt1.range_sum(b + 1, le)) bit1.add(b, a) cnt1.add(b, 1) print(ans)