class SegTree: """ init(init_val, ide_ele): 配列init_valで初期化 O(N) update(k, x): k番目の値をxに更新 O(logN) query(l, r): 区間[l, r)をsegfuncしたものを返す O(logN) """ def __init__(self, init_val, segfunc, ide_ele): """ init_val: 配列の初期値 segfunc: 区間にしたい操作 ide_ele: 単位元 n: 要素数 num: n以上の最小の2のべき乗 tree: セグメント木(1-index) """ n = len(init_val) self.segfunc = segfunc self.ide_ele = ide_ele self.num = 1 << (n - 1).bit_length() self.tree = [ide_ele] * 2 * self.num # 配列の値を葉にセット for i in range(n): self.tree[self.num + i] = init_val[i] # 構築していく for i in range(self.num - 1, 0, -1): self.tree[i] = self.segfunc(self.tree[2 * i], self.tree[2 * i + 1]) def update(self, k, x): """ k番目の値をxに更新 k: index(0-index) x: update value """ k += self.num self.tree[k] = x while k > 1: self.tree[k >> 1] = self.segfunc(self.tree[k], self.tree[k ^ 1]) k >>= 1 def query(self, l, r): """ [l, r)のsegfuncしたものを得る l: index(0-index) r: index(0-index) """ res = self.ide_ele l += self.num r += self.num while l < r: if l & 1: res = self.segfunc(res, self.tree[l]) l += 1 if r & 1: res = self.segfunc(res, self.tree[r - 1]) l >>= 1 r >>= 1 return res def value(self,index): return self.query(index,index+1) from collections import defaultdict def compress(A): tmp = defaultdict(int) B = list(set(A)) B.sort() zipped = [] #i番目のAの要素は何番目に大きいか unzipped = [] # i番目に大きいAの要素は何か for index,b in enumerate(B): tmp[b] = index unzipped.append(b) for a in A: zipped.append(tmp[a]) return zipped,unzipped N = int(input()) A = list(map(int,input().split())) mod = 10 ** 9 + 7 zipped,unzipped = compress(A) m = len(unzipped) segl = SegTree([0] * m,lambda x,y:(x+y)%mod,0) ldown = [] lup = [] for i in range(N): suml = segl.query(0,zipped[i]) ldown.append(suml) suml = segl.query(zipped[i]+1,m) lup.append(suml) segl.update(zipped[i],(segl.value(zipped[i]) + pow(2,i,mod))%mod) segr = SegTree([0] *m,lambda x,y:(x+y)%mod,0) rdown = [] rup = [] for i in reversed(range(N)): sumr = segr.query(0,zipped[i]) rdown.append(sumr) sumr = segr.query(zipped[i]+1,m) rup.append(sumr) segr.update(zipped[i],(segr.value(zipped[i]) + pow(2,N-i-1,mod))%mod) rdown.reverse() rup.reverse() ans = 0 for i in range(N): ans += ldown[i] * rdown[i] ans %= mod ans += lup[i] * rup[i] ans %= mod print(ans)