提交时间:2022-10-15 11:29:39

运行 ID: 60395

#include<bits/stdc++.h> using namespace std; struct node{ int x,num; }b[500005]; bool cmp(node xx,node yy){ return xx.x!=yy.x?xx.x<yy.x:xx.num<yy.num; } long long n,a[500005],sum,mod=1000000007,nxt[500005]; int main(){ cin>>n; for(int i=1;i<=n;i++){ cin>>a[i]; sum=(sum+((a[i]*i)%mod*(n-i+1))%mod)%mod; b[i].x=a[i]; b[i].num=i; } sort(b+1,b+n+1,cmp); for(int i=1;i<n;i++) if(b[i].x==b[i+1].x) nxt[b[i].num]=b[i+1].num; for(int i=1;i<=n;i++) if(nxt[i])sum=(sum+mod-(a[i]*(i*(n-nxt[i]+1))%mod)%mod)%mod; cout<<sum<<endl; }