class SegmentTree(object): def __init__(self, A, dot, unit): n = 1 << (len(A) - 1).bit_length() tree = [unit] * (2 * n) for i, v in enumerate(A): tree[i + n] = v for i in range(n - 1, 0, -1): tree[i] = dot(tree[i << 1], tree[i << 1 | 1]) self._n = n self._tree = tree self._dot = dot self._unit = unit def __getitem__(self, i): return self._tree[i + self._n] def update(self, i, v): i += self._n self._tree[i] = v while i != 1: i >>= 1 self._tree[i] = self._dot(self._tree[i << 1], self._tree[i << 1 | 1]) def add(self, i, v,ope): self.update(i, ope(self[i],v)) def sum(self, l, r): #これで[l,r)から取り出す。 l += self._n r += self._n l_val = r_val = self._unit while l < r: if l & 1: l_val = self._dot(l_val, self._tree[l]) l += 1 if r & 1: r -= 1 r_val = self._dot(self._tree[r], r_val) l >>= 1 r >>= 1 return self._dot(l_val, r_val) from collections import defaultdict def compress(A): tmp = defaultdict(int) B = list(set(A)) B.sort() zipped = [] #i番目のAの要素は何番目に大きいか unzipped = [] # i番目に大きいAの要素は何か for index,b in enumerate(B): tmp[b] = index unzipped.append(b) for a in A: zipped.append(tmp[a]) return zipped,unzipped N = int(input()) A = list(map(int,input().split())) mod = 998244353 zipped,unzipped = compress(A) n = len(set(A)) ans = 0 cumJ = 0 cumI = 0 ope = lambda x,y:(x+y) % mod for i in range(N): ans -=(cumJ * pow(2,N-1-i))%mod * A[i] ans %=mod ''' ans += segJ.sum(0,zipped[i]) * pow(2,N-1-i) * A[i] ans %=mod ''' cumJ += pow(2,i,mod) cumJ %=mod rev = pow(2,mod-2,mod) for i in reversed(range(N)): ''' ans -= segI.sum(zipped[i]+1,n) * pow(2,N-1+i) * A[i] ans %=mod ''' ans += (cumI * pow(2,N-1+i)) % mod * A[i] ans %=mod cumI += pow(rev,i,mod) cumI %=mod print(ans)