1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
| #include <bits/stdc++.h> using namespace std; #define FO(x) freopen(#x".in","r",stdin),freopen(#x".out","w",stdout) #define fo(i,j,k) for(int i=(j),end_i=(k);i<=end_i;i++) #define ff(i,j,k) for(int i=(j),end_i=(k);i< end_i;i++) #define fd(i,j,k) for(int i=(j),end_i=(k);i>=end_i;i--) #define DEBUG(x) cerr<<#x<<"="<<x<<endl #define all(x) (x).begin(),(x).end() #define cle(x) memset(x,0,sizeof(x)) #define lowbit(x) ((x)&-(x)) #define ll long long #define ull unsigned ll #define db double #define lb long db #define pb push_back #define mp make_pair #define fi first #define se second inline int read() { int x=0; char ch=getchar(); bool f=0; for(;ch<'0'||ch>'9';ch=getchar()) if(ch=='-') f=1; for(;ch>='0'&&ch<='9';ch=getchar()) x=(x<<3)+(x<<1)+(ch^48); return f?-x:x; } #define CASET fo(___,1,read()) const ll mod=1e9+7; inline ll Add(ll x,ll y){x+=y; return (x<mod)?x:x-mod;} inline ll Dec(ll x,ll y){x-=y; return (x<0)?x+mod:x;} inline ll Mul(ll x,ll y){return x*y%mod;} inline ll Pow(ll x,ll y) { y%=(mod-1);ll ans=1;for(;y;y>>=1,x=x*x%mod)if(y&1) ans=ans*x%mod; return ans; } inline ll calc(int x) { return 1ll*x*(x+1)/2%mod; } inline ll calc(int x,int y) { return (calc(y)-calc(x-1)+mod)%mod; } const int N=1e5+5; #define lc (u<<1) #define rc (lc|1) #define ls lc,l,mid #define rs rc,mid+1,r ll s[N<<2][4],tag[N<<2][2]; inline void pushtag(int u,int l,int r,int opt,int x) { s[u][2]=s[u][opt^1]*x%mod; s[u][opt]=1ll*(r-l+1)*x%mod; tag[u][opt]=x; if(opt==0) s[u][3]=1ll*calc(l,r)*x%mod; } inline void pushdown(int u,int l,int r) { int mid=l+r>>1; fo(i,0,1) if(tag[u][i]) { pushtag(ls,i,tag[u][i]); pushtag(rs,i,tag[u][i]); tag[u][i]=0; } } inline void pushup(int u) { fo(i,0,3) s[u][i]=(s[lc][i]+s[rc][i])%mod; }
void update(int u,int l,int r,int L,int R,int opt,int x) { if(L<=l&&r<=R) return pushtag(u,l,r,opt,x); pushdown(u,l,r); int mid=l+r>>1; if(L<=mid) update(ls,L,R,opt,x); if(mid<R) update(rs,L,R,opt,x); pushup(u); } ll sum[4]; void ask(int u,int l,int r,int L,int R) { if(L<=l&&r<=R) { fo(i,1,3) sum[i]+=s[u][i]; return; } pushdown(u,l,r); int mid=l+r>>1; ll ans=0; if(L<=mid) ask(ls,L,R); if(mid<R) ask(rs,L,R); } int n,a[N],b[N],m; int las[N],pre[N],nex[N],st[N],top; ll ans; int f1[N],f2[N]; int main() { n=read(); fo(i,1,n) b[i]=a[i]=read(); sort(b+1,b+n+1); m=unique(b+1,b+n+1)-b-1; fo(i,1,n) a[i]=lower_bound(b+1,b+m+1,a[i])-b; fo(i,1,n) pre[i]=las[a[i]]+1,las[a[i]]=i; fo(i,1,n) las[i]=n+1; fd(i,n,1) nex[i]=las[a[i]]-1,las[a[i]]=i; st[top=0]=n+1; fd(i,n,1) { for(;top&&pre[i]>pre[st[top]];top--); f1[i]=st[top]-1; st[++top]=i; } st[top=0]=n+1; fd(i,n,1) { for(;top&&nex[i]<nex[st[top]];top--); f2[i]=st[top]-1; st[++top]=i; } fo(i,1,n) las[i]=0; int j=n; fd(i,n,1) { update(1,1,n,i,f1[i],0,pre[i]); update(1,1,n,i,f2[i],1,nex[i]); las[a[i]]++; for(;las[a[i]]>=2;las[a[j]]--,j--); fo(k,1,3) sum[k]=0; ask(1,1,n,i,j); fo(k,1,3) sum[k]%=mod; (ans+=1ll*i*(sum[1]-calc(i,j))-sum[2]+sum[3])%=mod; } printf("%lld",(ans+mod)%mod); return 0; }
|