class BIT: #0-indexed __slots__ = ["size", "tree","depth","n0"] def __init__(self, n): self.size = n self.tree = [0]*(n+1) self.depth = n.bit_length() self.n0 = 1< 0: s += self.tree[i] i -= i & -i return s def range_sum(self,l,r): #a_l + ... + a_r 閉区間 return self.get_sum(r) - self.get_sum(l-1) def range_sum_larger(self,l): #a_l + ... (端まで) return self.get_sum(self.size-1) - (self.get_sum(l-1) if l else 0) def add(self, i, x): i += 1 while i <= self.size: self.tree[i] += x i += i & -i def bisect_left(self,w): #和が w 以上になる最小の index #w が存在しない場合 self.size を返す if w <= 0: return 0 x,k = 0,self.n0 for _ in range(self.depth): k >>= 1 if x+k <= self.size and self.tree[x+k] < w: w -= self.tree[x+k] x += k return x import sys readline = sys.stdin.readline #n = int(readline()) #*a, = map(int,readline().split()) # b = [list(map(int,readline().split())) for _ in range()] n = int(readline()) *p, = map(int,readline().split()) b1 = BIT(n+1) b2 = BIT(n+2) ans = 0 MOD = 998244353 pow2 = [1] inv2 = (MOD+1)//2 for i in range(n+2): pow2.append(pow2[-1]*2%MOD) for i in range(n): s = b1.range_sum_larger(p[i]) t = b2.range_sum_larger(p[i]) ans += pow2[n-1]*s ans -= pow2[n-1-i]*t ans %= MOD b1.add(p[i],1) b2.add(p[i],pow2[i]) print(ans)