from collections import Counter, defaultdict mod = 998244353 class fenwick_tree(object): def __init__(self, n): self.n = n self.log = n.bit_length() self.data = [0] * n def __sum(self, r): s = 0 while r > 0: s += self.data[r - 1] r -= r & -r return s def add(self, p, x): """ a[p] += xを行う""" p += 1 while p <= self.n: self.data[p - 1] += x p += p & -p def sum(self, l, r): """a[l] + a[l+1] + .. + a[r-1]を返す""" return self.__sum(r) - self.__sum(l) def lower_bound(self, x): """a[0] + a[1] + .. a[i] >= x となる最小のiを返す""" if x <= 0: return -1 i = 0 k = 1 << self.log while k: if i + k <= self.n and self.data[i + k - 1] < x: x -= self.data[i + k - 1] i += k k >>= 1 return i N = int(input()) A = list(map(int, input().split())) memo = defaultdict(list) for i, a in enumerate(A): memo[a].append(i) key = sorted(memo.keys()) # 左にある自分より大きいやつを見る L_sum = [0] * N L_cnt = [0] * N bit_sum = fenwick_tree(N) bit_cnt = fenwick_tree(N) for k in key[::-1]: for i in memo[k]: L_sum[i] = bit_sum.sum(0, i) L_cnt[i] = bit_cnt.sum(0, i) for i in memo[k]: bit_sum.add(i, k) bit_cnt.add(i, 1) # 右にある自分より小さいやつを見る R_sum = [0] * N R_cnt = [0] * N bit_sum = fenwick_tree(N) bit_cnt = fenwick_tree(N) for k in key: for i in memo[k]: R_sum[i] = bit_sum.sum(i, N) R_cnt[i] = bit_cnt.sum(i, N) for i in memo[k]: bit_sum.add(i, k) bit_cnt.add(i, 1) ans = 0 for i in range(1, N - 1): ans += L_sum[i] * R_cnt[i] ans %= mod ans += R_sum[i] * L_cnt[i] ans %= mod ans += A[i] * R_cnt[i] * L_cnt[i] ans %= mod print(ans)