Description
Sereja has a sequence that consists of n positive integers, a1, a2, …, an.
First Sereja took a piece of squared paper and wrote all distinct non-empty non-decreasing subsequences of sequence a. Then for each sequence written on the squared paper, Sereja wrote on a piece of lines paper all sequences that do not exceed it.
A sequence of positive integers x = x1, x2, …, xr doesn’t exceed a sequence of positive integers y = y1, y2, …, yr, if the following inequation holds: x1 ≤ y1, x2 ≤ y2, …, xr ≤ yr.
Now Sereja wonders, how many sequences are written on the lines piece of paper. Help Sereja, find the required quantity modulo 1000000007(109 + 7).
Input
The first line contains integer n(1 ≤ n ≤ 105). The second line contains n integers a1, a2, …, an(1 ≤ ai ≤ 106).
Output
In the single line print the answer to the problem modulo 1000000007(109 + 7).
Sample Input
Input
1
42
Output
42
Input
3
1 2 2
Output
13
Input
5
1 2 3 4 5
Output
719
1,由于子序列不可以改变原有序列元素的相对位置,所以涉及到区间值查询,需要用到树状数组(下标从一开始);
2,其值即为每一位的乘积;
2,设以val为结尾(下标为pos)的种数为res[pos],则由于从小到大排序位于前面但大于Val的值依然为零,且前面没有与此重复的res[pos]=query(pos-1)*val+val;若有重复则res[pos]=[query(pos-1)-query(last-1)]**val;
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int Max=1e5+10,Max1=1e7+5,mod=1e9+7;
int n,last[Max1],res[Max];
struct node
{
int x,pos;
} s[Max];
int lowbit(int x)
{
return x&(-x);
}
void update(int i,int val)
{
while(i<=n) res[i]=(res[i]+val)%mod,i+=lowbit(i);
}
long long query(int pos)
{
long long cnt=0;
while(pos>=1) cnt=(cnt+res[pos])%mod,pos-=lowbit(pos);
return cnt%mod;
}
int cmp(node a,node b)//数值和下标均从小到大
{
if(a.x!=b.x)
return a.x<b.x;
return a.pos<b.pos;
}
int main()
{
cin>>n;
for(int i=1; i<=n; i++)
{
cin>>s[i].x;
s[i].pos=i;
}
sort(s+1,s+1+n,cmp);
long long ans=0;
for(int i=1; i<=n; i++)
{
long long q;
int val=s[i].x,pos=s[i].pos;
//cout<<"--------"<<val<<" "<<pos<<endl;
if(last[val]==0)
q=(query(pos-1)*val%mod+val)%mod;
else
q=(query(pos-1)-query(last[val]-1)+mod)%mod*val%mod;
ans=(ans+q)%mod;
update(pos,q);
last[val]=pos;
}
cout<<ans%mod<<endl;
return 0;
}