class SegmentTree: def __init__(self, init_val, segfunc, operate, ide_ele): n = len(init_val) self.segfunc = segfunc self.operate = operate self.ide_ele = ide_ele self.num = 1 << (n - 1).bit_length() self.tree = [ide_ele] * 2 * self.num self.range = [(-1,n)] * 2 * self.num # 配列の値を葉にセット for i in range(n): self.tree[self.num + i] = init_val[i] self.range[self.num + i] = (i,i) # 構築していく for i in range(self.num - 1, 0, -1): self.tree[i] = self.segfunc(self.tree[2 * i], self.tree[2 * i + 1]) self.range[i] = (self.range[2 * i][0],self.range[2 * i + 1][1]) def update(self, k, x): k += self.num self.tree[k] = self.operate(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): 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 __getitem__(self,idx): idx += self.num return self.tree[idx] mask = 2**30-1 mod = 10**9+7 def segfunc(x,y): val_x,l_x = x>>30,x&mask val_y,l_y = y>>30,y&mask if l_x < l_y: return y elif l_x > l_y: return x else: val = (val_x + val_y) % mod return (val<<30) + l_x def operate(x,y): if not y&1: return y>>1 else: y >>= 1 val_x,l_x = x>>30,x&mask val = (val_x + y) % mod return (val<<30) + l_x ide_ele = 0 N = int(input()) A = list(map(int,input().split())) val = list(set(A)) val.sort() comp = {i:e for e,i in enumerate(val)} A = [comp[A[i]] for i in range(N)] n = max(A) Seg = SegmentTree([0]*(n+1),segfunc,operate,ide_ele) for i in range(N): if A[i] == 0: if Seg[0]&mask == 0: val = (1<<30) + 1 Seg.update(0,val<<1) elif Seg[0]&mask == 1: val = (1<<1) + 1 Seg.update(0,val) else: res = Seg.query(0,A[i]) tmp,tmp_L = res>>30,res&mask tmp_L += 1 if tmp_L == 1: tmp = (tmp + 1) % mod if Seg[A[i]]&mask == tmp_L: val = (tmp<<1) + 1 Seg.update(A[i],val) elif Seg[A[i]]&mask < tmp_L: val = (tmp<<30) + tmp_L Seg.update(A[i],val<<1) res = Seg.query(0,n+1) print(res>>30)