import sys input = sys.stdin.readline from collections import defaultdict def compress(l): l = list(set(l)) l.sort() idx = defaultdict(int) for i in range(len(l)): idx[l[i]] = i return idx class BIT: def __init__(self, n): self.n = n self.bit = [0]*(n+1) def add(self, i, x): i += 1 while i<=self.n: self.bit[i] += x self.bit[i] %= MOD i += i&(-i) def acc(self, i): s = 0 while i>0: s += self.bit[i] i -= i&(-i) return s N = int(input()) A = list(map(int, input().split())) ans = 0 MOD = 10**9+7 pos = defaultdict(list) for i in range(N): pos[A[i]].append(i) ks = list(pos.keys()) ks.sort() bit1 = BIT(N) bit2 = BIT(N) for k in ks: for p in pos[k]: l = bit1.acc(p) r = bit2.acc(N)-bit2.acc(p) ans += l*r%MOD ans %= MOD for p in pos[k]: bit1.add(p, pow(2, p, MOD)) bit2.add(p, pow(2, N-1-p, MOD)) for p in pos[k]: l = pow(2, p, MOD)-1-bit1.acc(p) r = pow(2, N-p, MOD)-1-bit2.acc(N)+bit2.acc(p) ans += l*r%MOD ans %= MOD print(ans)