#include <bits/stdc++.h>
#define mod 1000000007
using namespace std;
typedef pair<int, long long> P;
 
long long n;
long long qq[200001];
vector<P> vc[200001];
 
void dfs(int pt, int par){
	for(auto i : vc[pt]){
		if(i.first==par) continue;
		qq[i.first]=qq[pt]^i.second;
		dfs(i.first, pt);
	}
}
 
int main(){
	long long a, b ,w;
	cin >> n;
	for(int i=1;i<n;i++){
		cin >> a >> b >> w;
		vc[a].push_back(make_pair(b, w));
		vc[b].push_back(make_pair(a, w));
	}
	dfs(1, 0);
	__int128 cnt[61]={0}, temp;
	for(int i=1;i<=n;i++){
		int ptr=0;
		temp=qq[i];
		while(temp>0){
			if(temp%2==1) cnt[ptr]++;
			temp/=2;
			ptr++;
		}
	}
	__int128 ans=0, mul=1;
	for(int i=1;i<n;i++){
		int ptr=0;
		temp=qq[i];
		for(int j=0;j<=60;j++){
			if(temp%2==1){
				cnt[ptr]--;
				ans+=(mul%mod)*(n-i-cnt[ptr])%mod;
			}
			else{
				ans+=(mul%mod)*(cnt[ptr])%mod;
			}
			temp/=2;
			ptr++;
			mul*=2;
			ans%=mod;
		}
		mul=1;
	}
	long long print=ans;
	cout<<print<<'\n';
}
//Bismillahirrahmanirrahim

#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")

#include <bits/stdc++.h>

using namespace std;

typedef long long lo;
typedef pair< lo,lo > PII;

#define fi first
#define se second
#define mp make_pair
#define endl "\n"
#define pb push_back
#define fio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define FOR for(int i=1;i<=n;i++)
#define mid ((start+end)/2)
#define ort ((bas+son)/2)

const lo inf = 1000000000000000000;
const lo KOK = 100000;
const lo LOG = 30;
const lo li = 500005;
const lo mod = 1000000007;

int n,m,b[li],a[li],k,flag,t,say,vis[li];
int cev;
string s;
vector<int> v[li];

inline void dfs(int node,int ata){
	if(vis[node]){flag=1;return ;}
	vis[node]=1;
	say++;
	for(auto go:v[node]){
		if(go==ata)continue;
		dfs(go,node);
	}
}

int main(void){
	scanf("%d",&n);
	FOR{
		int x,y;
		scanf("%d %d",&x,&y);
		v[x].pb(y);
		v[y].pb(x);
	}
	cev=0;
	for(int i=1;i<=400000;i++){
		if(vis[i])continue;
		flag=0;
		say=0;
		dfs(i,0);
		if(flag==0)cev+=say-1;
		else cev+=say;
	}
	printf("%d\n",cev);
	return 0;
}

