显示代码纯文本
#include<bits/stdc++.h>
#define maxn 100010
#define LL long long
using namespace std;
const LL mod=(LL)1000000007;
vector<int>b[maxn];
int n,a1,a2;
LL f[maxn],ans,w[maxn];
void dfs(int p,int fa,LL sum){
ans=(ans+sum*w[p])%mod;f[p]=w[p];sum=(sum+w[p]*w[p])%mod;
for(int i=0;i<b[p].size();i++){
int x=b[p][i];if(x==fa)continue;
dfs(x,p,sum);sum=(sum+f[x]*w[p])%mod;f[p]=(f[p]+f[x])%mod;
}
return;
}
int main(){
freopen("asm_algo.in","r",stdin);
freopen("asm_algo.out","w",stdout);
scanf("%d%lld",&n,&w[1]);
for(int i=2;i<=n;i++){a2=i;
scanf("%d%lld",&a1,&w[i]);
b[a1].push_back(a2);
b[a2].push_back(a1);
}
dfs(1,0,(LL)0);ans=(ans*(LL)2)%mod;
for(int i=1;i<=n;i++)ans=(ans+w[i]*w[i]%mod*w[i]%mod)%mod;
printf("%lld\n",ans);
return 0;
}