#先頭からのsum,addをO(logN) class Fenwick_Tree: def __init__(self, n): self._n = n # 要素数 self.data = [0] * n def add(self, p, x): assert 0 <= p < self._n p += 1 # 0-indexed -> 1-indexed while p <= self._n: self.data[p - 1] += x # dataを更新 p += p & -p # pにLSB(p)を加算,data[i]の長さを示す def _sum(self, r):#s = a0+a1+...+a[r-1] or 0 s = 0 # 総和を入れる変数 while r > 0: s += self.data[r - 1] r -= r & -r # rからLSB(r)を減算 return s def sum(self, l, r):#[l,r)の総和 assert 0 <= l <= r <= self._n return self._sum(r) - self._sum(l) mod = 10**9+7 n = int(input()) a = list(map(int,input().split())) a2b = {key:idx for idx,key in enumerate(sorted(set(a)))} b = [a2b[a[i]] for i in range(n)] if n <= 2: print(0) exit() pow2 = [1] for i in range(n): pow2.append(pow2[-1]*2%mod) table = [[0 for _ in range(n)] for _ in range(4)] fw = [Fenwick_Tree(n+1) for _ in range(4)] for i in range(n): table[0][i] = fw[0].sum(0,b[i]) % mod table[1][i] = fw[1].sum(b[i]+1,n+1) % mod fw[0].add(b[i],pow2[i]) fw[1].add(b[i],pow2[i]) for i in range(n): table[2][-1-i] = fw[2].sum(0,b[-1-i]) % mod table[3][-1-i] = fw[3].sum(b[-1-i]+1,n+1) % mod fw[2].add(b[-1-i],pow2[i]) fw[3].add(b[-1-i],pow2[i]) ans = 0 for i in range(1,n-1): ans += table[0][i] * table[2][i] ans += table[1][i] * table[3][i] ans %= mod print(ans)