import sys def I(): return int(sys.stdin.readline().rstrip()) def LI(): return list(map(int,sys.stdin.readline().rstrip().split())) class SegTree: # モノイドに対して適用可能、Nが2冪でなくても良い def __init__(self,N,seg_func,unit): self.N = 1 << (N-1).bit_length() self.func = seg_func self.unit = unit self.tree = [self.unit]*(2*self.N) def set_val(self,i,x): # i番目(0-index)の値をxに変更 i += self.N self.tree[i] = x i >>= 1 while i: self.tree[i] = self.func(self.tree[i << 1],self.tree[i << 1 | 1]) i >>= 1 def fold(self,L,R): # [L,R)の区間取得 L += self.N R += self.N vL = self.unit vR = self.unit while L < R: if L & 1: vL = self.func(vL,self.tree[L]) L += 1 if R & 1: R -= 1 vR = self.func(self.tree[R],vR) L >>= 1 R >>= 1 return self.func(vL,vR) def seg_func(x,y): x0,x1 = x y0,y1 = y return ((x0+y0) % mod,(x1+y1) % mod) N = I() A = LI() mod = 998244353 AA = sorted(list(set(A))) d = {} M = len(AA) for i in range(M): d[AA[i]] = i ST_left = SegTree(N,seg_func,(0,0)) X_left = [] for i in range(N): a = d[A[i]] ST_left.set_val(a,seg_func((A[i],1),ST_left.tree[a+ST_left.N])) if 1 <= i <= N-2: X_left.append(ST_left.fold(a+1,N)) ST_right = SegTree(N,seg_func,(0,0)) X_right = [] for i in range(N-1,-1,-1): a = d[A[i]] ST_right.set_val(a,seg_func((A[i],1),ST_right.tree[a+ST_right.N])) if 1 <= i <= N-2: X_right.append(ST_right.fold(0,a)) X_right.reverse() ans = 0 for i in range(1,N-1): a = A[i] x,y = X_left[i-1] z,w = X_right[i-1] if y > 0 and w > 0: ans += a*(y*w)+w*x+y*z ans %= mod print(ans)