import typing import sys import math import collections import bisect import itertools import heapq import decimal import copy # sys.setrecursionlimit(10000001) INF = 10 ** 20 # MOD = 10 ** 9 + 7 MOD = 998244353 def ni(): return int(sys.stdin.buffer.readline()) def ns(): return map(int, sys.stdin.buffer.readline().split()) def na(): return list(map(int, sys.stdin.buffer.readline().split())) def na1(): return list(map(lambda x: int(x)-1, sys.stdin.buffer.readline().split())) # ===CODE=== # https://ikatakos.com/pot/programming_algorithm/dynamic_programming/inversion class Bit: 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 def main(): n = ni() a = na() sa = sorted(list(set(a))) d = {ai: i+1 for i, ai in enumerate(sa)} cnt = Bit(n) val = Bit(n) cntRes = [0]*n valRes = [0]*n for i, ai in enumerate(a): idx = d[ai] cntRes[i] = cnt.sum(n)-cnt.sum(idx) valRes[i] = val.sum(n)-val.sum(idx) cnt.add(idx, 1) val.add(idx, ai) ans = 0 cnt = Bit(n) val = Bit(n) for i in range(n-1, -1, -1): idx = d[a[i]] c = cnt.sum(idx-1) v = val.sum(idx-1) cnt.add(idx,1) val.add(idx,a[i]) ans += a[i]*cntRes[i]*c ans += c*valRes[i] ans += v*cntRes[i] ans %= MOD print(ans) if __name__ == '__main__': main()