from bisect import bisect_left import sys int1 = lambda x: int(x) - 1 # input = lambda: sys.stdin.buffer.readline() input = lambda: sys.stdin.readline().rstrip() ii = lambda: int(input()) i1 = lambda: int1(input()) mi = lambda: map(int, input().split()) mi1 = lambda: map(int1, input().split()) li = lambda: list(mi()) li1 = lambda: list(mi1()) lli = lambda n: [li() for _ in range(n)] INF = float("inf") mod = int(1e9 + 7) # mod = 998244353 class fenwick_tree: n = 1 data = [0 for i in range(n)] def __init__(self, N): self.n = N self.data = [0 for i in range(N)] def add(self, p, x): assert 0 <= p < self.n, "0<=p= mod: self.data[p - 1] -= mod p += p & -p def sum(self, l, r): assert 0 <= l and l <= r and r <= self.n, "0<=l<=r<=n,l={0},r={1},n={2}".format( l, r, self.n ) res = self.sum0(r) - self.sum0(l) if res < 0: res += mod elif res >= mod: res -= mod return res def sum0(self, r): s = 0 while r > 0: s += self.data[r - 1] if s < 0: s += mod elif s >= mod: s -= mod r -= r & -r return s n = ii() a = li() c = sorted(set(a)) s = len(c) p2 = [pow(2, i, mod) for i in range(n)] l = fenwick_tree(s) r = fenwick_tree(s) for i in range(n): r.add(bisect_left(c, a[i]), p2[n - 1 - i]) ans = 0 for i in range(n): idx = bisect_left(c, a[i]) r.add(idx, mod - p2[n - 1 - i]) ans = ( ans + l.sum0(idx) * r.sum0(idx) + l.sum(idx + 1, s) * r.sum(idx + 1, s) ) % mod l.add(idx, p2[i]) print(ans)