#include <iostream>
#include <vector>
#include <cstdio>
#include <algorithm>
#include <set>
#include <map>
#include <cassert>
#include <numeric>
#include <string>
#include <cstring>
#include <cmath>
using namespace std;

#ifdef LOCAL
	#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
	#define eprintf(...) 42
#endif

typedef long long int int64;


const int N = (int) 1e5 + 100;
vector <int> ch[N];
int sz[N];
int ans[N];

void dfs(int v)
{
    ans[v] = 1;
    sz[v] = 1;
    vector <int> odd, even;
    for (int to : ch[v])
    {
        dfs(to);
        sz[v] += sz[to];
        if (sz[to] % 2)
            odd.push_back(ans[to]);
        else
            even.push_back(ans[to]);
    }
    sort(odd.begin(), odd.end());
    sort(even.begin(), even.end());
    // reverse(odd.begin(), odd.end());
    reverse(even.begin(), even.end());

    while (!even.empty() && even.back() < 0)
    {
        ans[v] += even.back();
        even.pop_back();
    }
    if ((int) odd.size() % 2 == 0) {
        for (int x : even)
            ans[v] += x;
    }
    else {
        for (int x : even)
            ans[v] -= x;
    }
    for (int i = 0; i < (int) odd.size(); i++)
        if (i % 2)
            ans[v] -= odd[i];
        else
            ans[v] += odd[i];

    eprintf("%d) %d %d\n", v, sz[v], ans[v]);
}


int main(int,  char **)
{
#ifdef LOCAL
	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
#endif

    int n;
    scanf("%d", &n);
    for (int i = 1; i < n; i++)
    {
        int p;
        scanf("%d", &p);
        p--;
        ch[p].push_back(i);
    }

    dfs(0);
    int a = (n - ans[0]) / 2;
    printf("%d\n", a + ans[0]);

	return 0;
}



#include<bits/stdc++.h>
#define N 200005
#define mod 1000000007
using namespace std;
typedef long long ll;
inline ll read(){
	ll x=0,f=1;char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')f=-f;ch=getchar();}
	while(isdigit(ch)){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
	return x*f;
}
inline void write(ll x){
	if(x<0)putchar('-'),x=-x;
	if(x>9)write(x/10);
	putchar(x%10+'0');
}
inline void writeln(ll x){write(x),putchar('\n');}
struct edge{ll to,nxt,w;}es[N<<1];
ll n,tot,hd[N],x[N],c[65];
inline void add(ll u,ll v,ll w){es[++tot]=(edge){v,hd[u],w},hd[u]=tot;}
inline void dfs(ll u,ll fa){
	for(ll i=hd[u],v;i;i=es[i].nxt){
		if((v=es[i].to)!=fa){
			x[v]=x[u]^es[i].w;
			dfs(v,u);
		}
	}
}
inline ll cal(ll num,ll pos){return num&(1ll<<pos);}
inline ll pw(ll a,ll b){
	ll res=1;
	while(b){
		if(b&1)res=res*a%mod;
		a=a*a%mod,b>>=1;
	}
	return res;
}
int main(){
	n=read();
	for(ll i=1,u,v,w;i<n;i++){
		u=read(),v=read(),w=read();
		add(u,v,w),add(v,u,w);
	}
	dfs(1,0);
	for(ll i=0;i<=60;i++)
		for(ll j=1;j<=n;j++)
			if(cal(x[j],i))c[i]++;
	ll ans=0;
	for(ll i=0;i<=60;i++)
		ans=(ans+(c[i]*(n-c[i])%mod)*pw(2,i)%mod)%mod;
	writeln(ans);
	return 0;
}