N = int(input()) A = list(map(int,input().split())) mod = 998244353 class Bit1: 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] s %= mod i -= i & -i return s def add(self, i, x): while i <= self.size: self.tree[i] += x self.tree[i] %= mod i += i & -i class Bit2: 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 return s def add(self, i, x): while i <= self.size: self.tree[i] += x i += i & -i B = list(set(A)) B.sort() M = len(B) D = dict(zip(B,range(M))) bit11 = Bit1(M) bit21 = Bit1(M) bit12 = Bit2(M) bit22 = Bit2(M) ans = 0 for i in range(N): tmp = bit21.sum(M) - bit21.sum(D[A[i]]+1) cnt = bit22.sum(M) - bit22.sum(D[A[i]]+1) if cnt > 0: ans = (ans + A[i] * cnt + tmp) % mod tmp = bit11.sum(M) - bit11.sum(D[A[i]]+1) cnt = bit12.sum(M) - bit12.sum(D[A[i]]+1) if cnt > 0: bit21.add(D[A[i]]+1,tmp + A[i] * cnt) bit22.add(D[A[i]]+1,cnt) bit11.add(D[A[i]]+1,A[i]) bit12.add(D[A[i]]+1,1) print(ans)