这样是O(n^2)的, 我们考虑像最短路计数一样(好像差不多)
如果f[j] 可以更新f[i] , 那么就把cnt[i]改成cnt[j], 如果刚好一样,就累加, 否则直接跳过
我们重载一下运算符, 树状数组维护
#include<bits/stdc++.h>
#define N 100050
#define LL long long
#define P 1000000007
using namespace std;
int n,a[N],b[N];
struct Node{
int len; LL cnt;
void operator += (const Node &b){
if(b.len < len) return;
else if(b.len > len) cnt = b.cnt, len = b.len;
else cnt = (cnt + b.cnt) % P;
}
}f[N],c[N],ans;
Node Quary(int x){
Node tmp = {0,1};
for(;x;x-=x&-x) tmp += c[x];
return tmp;
}
void Update(int x,Node val){
for(;x<=n;x+=x&-x) c[x] += val;
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]); b[i] = a[i];
} sort(b+1,b+n+1);
int siz = unique(b+1,b+n+1) - (b+1);
for(int i=1;i<=n;i++) a[i] = lower_bound(b+1,b+siz+1,a[i]) - b;
for(int i=1;i<=n;i++){
f[i] = Quary(a[i]-1);
f[i].len ++;
ans += f[i];
Update(a[i],f[i]);
} printf("%lld",ans.cnt); return 0;
}