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

运行 ID: 60401

#include<bits/stdc++.h> #define mod 1000000007 #define int long long using namespace std; inline int read(){ int x=0,f=1; char c=getchar(); while(c<'0'||c>'9'){if(!(c^'-'))f=-1;c=getchar();} while(c>='0'&&c<='9')x=(x<<1)+(x<<3)+(c&15),c=getchar(); return x*f; } int a[500005],f[500005],n,ans; set<int>s; signed main(){ n=read(); for(int i=1;i<=n;i++)f[i]=read(),s.insert(f[i]); int now=n; for(int i=1,j=n;i<=(n>>1);i++,j--){ a[i]=a[j]=a[i-1]+now; now-=2; } if(n&1)a[(n>>1)+1]=a[(n>>1)]+1; if(s.size()==n){ for(int i=1;i<=n;i++)(ans+=(a[i]%mod*f[i]%mod))%=mod; printf("%lld\n",ans%mod); }else{ for(int i=2;i<=n;i++){ int cnt=0; for(int j=1;j<i;j++)if(f[i]==f[j])++cnt; a[i]-=cnt*(n-i+1); } for(int i=1;i<=n;i++)(ans+=(a[i]%mod*f[i]%mod))%=mod; printf("%lld\n",ans%mod); } return 0; }