from collections import * from functools import * from itertools import * from heapq import * import sys,math input = sys.stdin.readline class SegTree: """ Segment Tree """ def __init__(self, init_val, segfunc, ide_ele): """ 初期化 init_val: 配列の初期値 """ n = len(init_val) self.segfunc = segfunc self.ide_ele = ide_ele self.num = 1 << (n - 1).bit_length() self.tree = [ide_ele] * 2 * self.num # 配列の値を葉にセット for i in range(n): self.tree[self.num + i] = init_val[i] # 構築していく for i in range(self.num - 1, 0, -1): self.tree[i] = segfunc(self.tree[2 * i], self.tree[2 * i + 1]) def update(self, k, x): """ k番目の値をxに更新 k: index(0-index) x: update value """ k += self.num self.tree[k] = x while k > 1: self.tree[k >> 1] = self.segfunc(self.tree[k], self.tree[k ^ 1]) k >>= 1 def query(self, l, r): """ [l, r)のsegfuncしたものを得る l: index(0-index) r: index(0-index) """ res = self.ide_ele l += self.num r += self.num while l < r: if l & 1: res = self.segfunc(res, self.tree[l]) l += 1 if r & 1: res = self.segfunc(res, self.tree[r - 1]) l >>= 1 r >>= 1 return res N = int(input()) A = list(map(int,input().split())) mod = 998244353 Z = list(set(A)) Z.sort() inv = {z:i for i,z in enumerate(Z)} def segfunc(x,y): return (x+y)%mod n = len(Z) X0 = [0]*n X1 = [0]*n Y0 = [0]*n Y1 = [0]*n L0 = SegTree([0]*n,segfunc,0) L1 = SegTree([0]*n,segfunc,0) R0 = SegTree([0]*n,segfunc,0) R1 = SegTree([0]*n,segfunc,0) def add_X(i): a = A[i] idx = inv[a] X0[idx] += 1 X1[idx] += a L0.update(idx,X0[idx]) L1.update(idx,X1[idx]) def del_Y(i): a = A[i] idx = inv[a] Y0[idx] -= 1 Y1[idx] -= a R0.update(idx,Y0[idx]) R1.update(idx,Y1[idx]) def add_Y(i): a = A[i] idx = inv[a] Y0[idx] += 1 Y1[idx] += a R0.update(idx,Y0[idx]) R1.update(idx,Y1[idx]) add_X(0) for i in range(2,N): add_Y(i) ans = 0 for i in range(1,N-1): a = A[i] idx = inv[a] ans += L0.query(idx+1,n)*R1.query(0,idx) + L1.query(idx+1,n)*R0.query(0,idx) + a*L0.query(idx+1,n)*R0.query(0,idx) ans %= mod add_X(i) del_Y(i+1) print(ans)