class BinaryIndexedTree: def __init__(self, n, mod): self.size = n self.tree = [0]*(n+1) self.mod = mod def add(self, i, x): while i <= self.size: self.tree[i] = (self.tree[i]+x)%self.mod i += i&-i def sum(self, i): s = 0 while i > 0: s = (s+self.tree[i])%self.mod i -= i&-i return s n = int(input()) a = list(map(int, input().split())) mod = 10**9+7 itov = sorted(set(a)) vtoi = {v: i for i, v in enumerate(itov)} b = [vtoi[v]+1 for v in a] r = [pow(2, i, mod) for i in range(n)] ldans = [0]*n luans = [0]*n ldbit = BinaryIndexedTree(n, mod) lubit = BinaryIndexedTree(n, mod) rdans = [0]*n ruans = [0]*n rdbit = BinaryIndexedTree(n, mod) rubit = BinaryIndexedTree(n, mod) ans = 0 for i, v in enumerate(b): ldans[i] = ldbit.sum(v-1) luans[i] = lubit.sum(n-v) ldbit.add(v, r[i]) lubit.add(n+1-v, r[i]) for i, v in enumerate(b[::-1]): rdans[i] = rdbit.sum(v-1) ruans[i] = rubit.sum(n-v) rdbit.add(v, r[i]) rubit.add(n+1-v, r[i]) for i in range(n): ans = (ans+ldans[i]*rdans[n-1-i])%mod ans = (ans+luans[i]*ruans[n-1-i])%mod print(ans)