def main0(n,a): mod=10**9+7 ans=0 for i in range(n-2): for j in range(i+1,n-1): if a[i]<=a[j]:continue for k in range(j+1,n): if a[i]>a[j]>a[k]: ans+=a[i]+a[j]+a[k] ans%=mod return ans # 0-indexed binary indexed tree class BIT: def __init__(self, n): self.n = n self.data = [0]*(n+1) self.el = [0]*(n+1) # sum of [0,i) sum(a[:i]) def sum(self, i): if i==0:return 0 s = 0 while i > 0: s += self.data[i] i -= i & -i return s def add(self, i, x): i+=1 self.el[i] += x while i <= self.n: self.data[i] += x i += i & -i # sum of [l,r) sum(a[l:r]) def sumlr(self, i, j): return self.sum(j) - self.sum(i) # a[i] def get(self,i): i+=1 return self.el[i] def main1(n,a): mod=998244353 # あるiについてja[i]のものとその逆のものを調べる。 # 合計と個数がわかればOK ary0=[0]*n # ary0[i]:ja[i]となるものの個数 ary1=[0]*n # ary0[i]:ia[j]となるものの個数 pa=list(set(a)) pa.sort() ap={x:i for i,x in enumerate(pa)} bit=BIT(n) for i in range(n): ary0[i]=i-bit.sum(ap[a[i]]+1) bit.add(ap[a[i]],1) bit=BIT(n) for i in reversed(range(n)): ary1[i]=bit.sum(ap[a[i]]) bit.add(ap[a[i]],1) sary0=[0]*n # ary0[i]:ja[i]となるものの総和 sary1=[0]*n # ary0[i]:ia[j]となるものの総和 aa=[[i,x] for i,x in enumerate(a)] aa.sort(key=lambda x:x[1]) bit=BIT(n) for i,x in aa[::-1]: sary0[i]=bit.sum(i) bit.add(i,x) bit=BIT(n) tmp=0 for i,x in aa: sary1[i]=tmp-bit.sum(i) bit.add(i,x) tmp+=x ans=0 for i in range(n): ans+=ary0[i]*sary1[i] ans+=ary1[i]*sary0[i] ans+=ary0[i]*ary1[i]*a[i] ans%=mod return ans if __name__=='__main__': n=int(input()) a=list(map(int,input().split())) #ret0=main0(n,a) #print(ret0) ret1=main1(n,a) print(ret1)