提交时间:2024-07-19 12:58:23

运行 ID: 30394

#include<bits/stdc++.h> #define up(i,l,r) for(int i=(l);i<=(r);++i) #define down(i,l,r) for(int i=(l);i>=(r);--i) #define pi pair<int,int> #define p1 first #define p2 second #define m_p make_pair #define p_b push_back typedef long long ll; using namespace std; const int maxn=2e5+10,mod=1e9+7; inline ll read(){ ll x=0;short t=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')t=-1;ch=getchar();} while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar(); return x*t; } int n,dfn[maxn],siz[maxn],son[maxn],top[maxn],fa[maxn],dep[maxn],ct; vector<int>v[maxn],INS[maxn],DEL[maxn]; int L[maxn],R[maxn],f[maxn]; int qpow(int a,int b){ int res=1; while(b){ if(b&1)res=res*1ll*a%mod; a=a*1ll*a%mod;b>>=1; }return res; } void dfs(int u){ siz[u]=1,son[u]=0; for(int x:v[u])if(x!=fa[u]){fa[x]=u,dep[x]=dep[u]+1;dfs(x);siz[u]+=siz[x];if(siz[x]>siz[son[u]])son[u]=x;} } void dfs2(int u,int tp){ dfn[u]=++ct,top[u]=tp; if(!son[u])return;dfs2(son[u],tp); for(int x:v[u])if(x!=son[u]&&x!=fa[u])dfs2(x,x); } struct SegTree { struct node { int lz,sm,len; }d[maxn<<2]; #define ls(p) (p<<1) #define rs(p) (p<<1|1) #define lz(p) d[p].lz #define sm(p) d[p].sm #define len(p) d[p].len void pu(int p){sm(p)=sm(ls(p))+sm(rs(p));sm(p)%=mod;} void cl(int p,int x){(lz(p)+=x)%=mod,(sm(p)+=x*1ll*len(p)%mod)%=mod;} void pd(int p){cl(ls(p),lz(p)),cl(rs(p),lz(p)),lz(p)=0;} void bd(int l,int r,int p){ len(p)=r-l+1; if(l==r)return; int mid=l+r>>1; bd(l,mid,ls(p)),bd(mid+1,r,rs(p)); } void upd(int l,int r,int s,int t,int p,int x){ if(l<=s&&t<=r){cl(p,x);return;}pd(p); int mid=s+t>>1; if(l<=mid)upd(l,r,s,mid,ls(p),x);if(r>=mid+1)upd(l,r,mid+1,t,rs(p),x);pu(p); }int qry(int l,int r,int s,int t,int p){ if(l<=s&&t<=r)return sm(p);pd(p); int mid=s+t>>1,res=0; if(l<=mid)(res+=qry(l,r,s,mid,ls(p)))%=mod;if(r>=mid+1)(res+=qry(l,r,mid+1,t,rs(p)))%=mod; return res; } }T; void upd(int u,int k){ while(u){ T.upd(dfn[top[u]],dfn[u],1,n,1,k); u=fa[top[u]]; } } int qry(int u){ int res=0; while(u){ res+=T.qry(dfn[top[u]],dfn[u],1,n,1);res%=mod; u=fa[top[u]]; }return res; } void slv(){ n=read();up(i,1,n){int l=read(),r=read();L[i]=l,R[i]=r,f[i]=r-l+1;INS[l].p_b(i),DEL[r+1].p_b(i);}T.bd(1,n,1); up(i,1,n-1){int x=read(),y=read();v[x].p_b(y),v[y].p_b(x);}dep[1]=1;dfs(1),dfs2(1,1); int res=0,all=0; int P=1;up(i,1,n)P=P*1ll*(R[i]-L[i]+1)%mod; int s1=0,s2=0; up(i,1,1e5){ for(int x:INS[i]){ (res+=dep[x]*1ll*qpow(f[x],mod-2)%mod*1ll*s1%mod)%=mod; (res+=s2*1ll*qpow(f[x],mod-2)%mod)%=mod; (s1+=P*1ll*qpow(f[x],mod-2)%mod)%=mod; (s2+=dep[x]*1ll*P%mod*1ll*qpow(f[x],mod-2)%mod)%=mod; int g=qry(x)*2%mod*1ll*qpow(f[x],mod-2)%mod; //cerr<<"!!! "<<g<<endl; res=(res-g+mod)%mod; upd(x,P*1ll*qpow(f[x],mod-2)%mod); } for(int x:DEL[i]){ upd(x,mod-P*1ll*qpow(f[x],mod-2)%mod); int g=qry(x)*2%mod*1ll*qpow(f[x],mod-2)%mod; res=(res+g)%mod; s2=(s2-dep[x]*1ll*P%mod*1ll*qpow(f[x],mod-2)%mod+mod)%mod; s1=(s1-P*1ll*qpow(f[x],mod-2)%mod+mod)%mod; res=(res-s2*1ll*qpow(f[x],mod-2)%mod+mod)%mod; res=(res-dep[x]*1ll*qpow(f[x],mod-2)%mod*1ll*s1%mod+mod)%mod; }(all+=res)%=mod; //if(i==1)cerr<<"now: "<<res<<endl; }cout<<all; }int main(){ slv(); //cerr<<clock()*1.0/CLOCKS_PER_SEC<<"s\n"; fclose(stdin); fclose(stdout); return 0; } //(depx+depy-2depL)P