提交时间:2022-10-15 11:32:31
运行 ID: 60425
#include<bits/stdc++.h> using namespace std; const int N =1e6+10; const int MAXN=1e9+10; const int mod=1e9+7; #define int long long set<int>s; vector<int> g[N<<1]; int a[N],sum[N]; signed main() { //freopen("J3.in","r",stdin); //freopen("J3.out","w",stdout); int n; cin>>n; for(int i=1;i<=n;i++) cin>>a[i],sum[i]=(sum[i-1]+a[i])%mod,s.insert(a[i]); if(s.size()==n) cout<<(2*sum[n]%mod)+((n-2)*(a[1]+a[n])%mod)+((sum[n]-a[1]-a[n])*(n-2)*2%mod)%mod<<endl; else { int ans=0; for(int i=1;i<=n;i++) { for(int j=i-1;j>=1;j--) { if((j==i-1||(j==1&&i==n))&&(a[i]==a[j])) ans=(ans+(a[i]*(n-i+j))%mod); else if(a[j]==a[i]&&j!=i-1) { ans=(ans+(a[i]*(n-i+j-(i-j-1)))%mod); } } } int ans2=(2*sum[n]%mod)+((n-2)*(a[1]+a[n])%mod)+(((sum[n]-a[1]-a[n])*(n-2))%mod*2)%mod; cout<<ans2-ans<<endl; } return 0; }