提交时间:2022-10-15 11:26:13

运行 ID: 60336

#include<bits/stdc++.h> using namespace std; const int M=1e9+7; const int N1=5e5+3; const int N2=32554303; int t[N1],a[N1],b[N2]; int Read(){ int sum=0; char ch=getchar(); while(ch<'0' || ch>'9') ch=getchar(); while(ch>='0' && ch<='9') sum=sum*10+ch-'0',ch=getchar(); return sum; } int main(){ // freopen("T3sample.in","r",stdin); int n; long long ans=0; n=Read(); for(int i=1;i<=n;++i) t[i]=i*(n-i+1); for(int i=1;i<=n;++i) { a[i]=Read(); if(a[i]<N2) { if(b[a[i]]) ans=(ans-b[a[i]]*(n-i+1)*a[i]+M)%M; b[a[i]]=i; } else for(int j=i;j;j--) if(a[j]==a[i]) { ans=(ans-j*(n-i+1)*a[i]+M)%M; break; } ans=(ans+a[i]*t[i])%M; } cout<<ans<<'\n'; return 0; }