显示代码纯文本
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#define LL unsigned long long
#define Inf 0x7f7f7f7f
using namespace std;
const int mod=1e9+7;
const int maxn=150000;
struct Edge{
int next,to;
} e[maxn<<4];
int n,len,head[maxn],root[maxn];
LL v[maxn];
LL sum[maxn],ans;
void Init();
inline void Insert(int x,int y){
len++;e[len].to=y;e[len].next=head[x];head[x]=len;
}
inline void Dfs(int x){
sum[x]=v[x];
for(int i=head[x];i;i=e[i].next){
int j=e[i].to;
if(root[x]!=j){
root[j]=x;
Dfs(j);
ans+=(LL)sum[x]%mod*sum[j]%mod*v[x]%mod;ans%=mod;
sum[x]=(LL)(sum[x]+sum[j])%mod;sum[x]%=mod;
}
}
}
int main(){
freopen("asm_algo.in","r",stdin);freopen("asm_algo.out","w",stdout);
Init();
//system("pause");
return 0;
}
inline void Init(){
scanf("%d%d",&n,&v[1]);
LL tot=0;tot+=v[1]%mod*v[1]%mod*v[1]%mod;tot%=mod;
for(int i=2;i<=n;i++){
int x;scanf("%d%llu",&x,&v[i]);
Insert(x,i);Insert(i,x);
tot+=(LL)v[i]%mod*v[i]%mod*v[i]%mod;
tot%=mod;
}
tot%=mod;
Dfs(1);
ans=ans%mod*2%mod;ans%=mod;ans=(ans+tot)%mod;
printf("%llu",(LL)ans%mod);
}