# Reference: https://ikatakos.com/pot/programming_algorithm/data_structure/binary_indexed_tree # Fenwick Tree # 0-indexed class BinaryIndexedTree: # a is virtual array # a = [0] * n def __init__(self, n, mod): self.size = n self.data = [0] * (n+1) self.mod = mod # return sum(a[0:i] % mod) def query(self, i): res = 0 while i > 0: res += self.data[i] res %= self.mod i -= i & -i return res # a[i] += x def add(self, i, x): i += 1 while i <= self.size: self.data[i] += x self.data[i] %= self.mod i += i & -i def debug(self): print([self.query(i+1)-self.query(i) for i in range(self.size)]) mod = 998244353 def main(): n = int(input()) a = list(map(int, input().split())) b = list(sorted(enumerate(a), key=lambda x: x[1])) a = [(0, 0)] * n for i in range(n): a[b[i][0]] = (i, b[i][1]) s1 = BinaryIndexedTree(n, mod) cnt1 = BinaryIndexedTree(n, mod) s2 = BinaryIndexedTree(n, mod) cnt2 = BinaryIndexedTree(n, mod) ans = 0 for i, x in a[::-1]: ans += cnt2.query(i)*x+s2.query(i) s2.add(i, cnt1.query(i)*x+s1.query(i)) cnt2.add(i, cnt1.query(i)) s1.add(i, x) cnt1.add(i, 1) print(ans % mod) main()