class Bit: def __init__(self, n): self.size = n self.tree = [0] * (n + 1) self.depth = n.bit_length() def sum(self, i): s = 0 while i > 0: s += self.tree[i] i -= i & -i return s def add(self, i, x): while i <= self.size: self.tree[i] += x i += i & -i def lower_bound(self, x): #xを超えない最大のiを返す sum_ = 0 pos = 0 for i in range(self.depth, -1, -1): k = pos + (1 << i) if k <= self.size and sum_ + self.tree[k] < x: sum_ += self.tree[k] pos += 1 << i return pos + 1, sum_ N=int(input()) A=[int(x) for x in input().split()] D={j:i for i,j in enumerate(sorted(set(A)))} E={i:j for i,j in enumerate(sorted(set(A)))} M=len(D) left_up=[0]*N left_down=[0]*N right_up=[0]*N right_down=[0]*N L=Bit(M) R=Bit(M) l=1 mod=10**9+7 for i in range(N): left_down[i]=L.sum(D[A[i]]) left_up[i]=L.sum(M)-L.sum(D[A[i]]+1) L.add(D[A[i]]+1,l) l=l*2%mod r=1 for i in range(N): j=-i-1 right_down[j]=R.sum(D[A[j]]) right_up[j]=R.sum(M)-R.sum(D[A[j]]+1) R.add(D[A[j]]+1,r) r=r*2%mod print(sum([(left_up[i]*right_up[i]+left_down[i]*right_down[i])%mod for i in range(N)])%mod)