import sys import sys import sys def main(): import sys import sys sys.setrecursionlimit(1 << 25) MOD = 998244353 class BIT: def __init__(self, size): self.N = size self.tree = [0] * (self.N + 2) def update(self, idx, val): while idx <= self.N: self.tree[idx] = (self.tree[idx] + val) % MOD idx += idx & -idx def query(self, idx): res = 0 while idx > 0: res = (res + self.tree[idx]) % MOD idx -= idx & -idx return res def range_query(self, l, r): if l > r: return 0 return (self.query(r) - self.query(l -1)) % MOD input = sys.stdin.read data = input().split() N = int(data[0]) P = list(map(int, data[1:N+1])) pow2 = [1] * (N+1) for k in range(1, N+1): pow2[k] = (pow2[k-1] *2) % MOD # Compute K: inversion count bit_inv = BIT(N) K =0 for j in range(N): val = P[j] cnt = bit_inv.range_query(val +1, N) K = (K + cnt) % MOD bit_inv.update(val,1) # Compute sum_T bit_sum_T = BIT(N) sum_T =0 for j in range(N): val = P[j] sum_i = bit_sum_T.range_query(val +1, N) term_j = (sum_i * pow2[N -j -1]) % MOD sum_T = (sum_T + term_j) % MOD bit_sum_T.update(val, pow2[j]) # Compute answer ans = (K * pow2[N-1] - sum_T) % MOD if ans <0: ans += MOD print(ans) if __name__ == "__main__": main()