#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <iomanip>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <unordered_map>
#include <bitset>
#include <chrono>
#include <random>
#define rep(i,n) for(int i=0;i<n;i++)
#define repn(i,n) for(int i=1;i<=n;i++)
#define repr(e,x) for(auto& e:x)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> P;
// typedef pair<int,P> IP;
// typedef pair<P,P> PP;
double const PI=3.141592653589793;
int const INF=1001001001;
ll const LINF=1001001001001001001;
ll const MOD=1000000007;

ll N;

void dfs(int v, int p, ll d, vector<ll> &dist, vector<vector<P>> &G){
    dist[v]=d;
    repr(e,G[v]){
        if(e.first!=p) dfs(e.first,v,d^e.second,dist,G);
    }
}

int main(){
    cin>>N;
    vector<vector<P>> G(N,vector<P>());
    rep(i,N-1){
        ll u,v,w;
        cin>>u>>v>>w;
        u--, v--;
        G[u].push_back({v,w});
        G[v].push_back({u,w});
    }

    vector<ll> dist(N,0);
    dfs(0,-1,0,dist,G);

    vector<ll> cnt(60,0);
    rep(i,N){
        rep(j,60) cnt[j]+=dist[i]>>j&1;
    }

    ll ans=0;
    ll two=1;
    rep(i,60){
        ans+=cnt[i]*(N-cnt[i])%MOD*two%MOD;
        ans%=MOD;
        two*=2;
        two%=MOD;
    }

    cout<<ans<<endl;
    
    return 0;
}
#pragma GCC optimize ("O2")
#pragma GCC target ("avx")
//#include<bits/stdc++.h>
//#include<atcoder/all>
//using namespace atcoder;
#include<iostream>
#include<cstring>

using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

//みんなよく解けるなぁ

const int CM = 1 << 17, CL = 20;
char cn[CM + CL], * ci = cn + CM + CL, * owa = cn + CM, ct;
const ll ma0 = 1157442765409226768;
const ll ma1 = 1085102592571150095;
const ll ma2 = 71777214294589695;
const ll ma3 = 281470681808895;
const ll ma4 = 4294967295;
const ll ma10 = ma1 & (1ll << 32) - 1;
inline int getint() {
	if (ci - owa > 0) {
		memcpy(cn, owa, CL);
		ci -= CM;
		fread(cn + CL, 1, CM, stdin);
	}
	ll tmp = *(ll*)ci;
	int dig = 68 - __builtin_ctzll((tmp & ma0) ^ ma0);
	tmp = tmp << dig & ma1;
	tmp = tmp * 10 + (tmp >> 8) & ma2;
	tmp = tmp * 100 + (tmp >> 16) & ma3;
	tmp = tmp * 10000 + (tmp >> 32) & ma4;
	ci += 72 - dig >> 3;
	return tmp;
}
const ll pre[9] = { 1,10,100,1000,10000,100000,1000000,10000000,100000000 };
inline ll getll_karini_sakusei() {
	if (ci - owa > 0) {
		memcpy(cn, owa, CL);
		ci -= CM;
		fread(cn + CL, 1, CM, stdin);
	}
	ll tmp = *(ll*)ci;
	if ((tmp & ma0) ^ ma0) {
		int dig = 68 - __builtin_ctzll((tmp & ma0) ^ ma0);
		tmp = tmp << dig & ma1;
		tmp = tmp * 10 + (tmp >> 8) & ma2;
		tmp = tmp * 100 + (tmp >> 16) & ma3;
		tmp = tmp * 10000 + (tmp >> 32) & ma4;
		ci += (72 - dig >> 3);
	}
	else {
		tmp = tmp & ma1;
		tmp = tmp * 10 + (tmp >> 8) & ma2;
		tmp = tmp * 100 + (tmp >> 16) & ma3;
		tmp = tmp * 10000 + (tmp >> 32) & ma4;
		ci += 8;
		ll tmp2 = *(ll*)ci;
		if ((tmp2 & ma0) ^ ma0) {
			int dig = 68 - __builtin_ctzll((tmp2 & ma0) ^ ma0);
			if (dig == 64) {
				ci++;
				return tmp;
			}
			tmp2 = tmp2 << dig & ma1;
			tmp2 = tmp2 * 10 + (tmp2 >> 8) & ma2;
			tmp2 = tmp2 * 100 + (tmp2 >> 16) & ma3;
			tmp2 = tmp2 * 10000 + (tmp2 >> 32) & ma4;
			ci += (72 - dig >> 3);
			tmp = tmp * pre[64 - dig >> 3] + tmp2;
		}
		else {
			tmp2 = tmp2 & ma1;
			tmp2 = tmp2 * 10 + (tmp2 >> 8) & ma2;
			tmp2 = tmp2 * 100 + (tmp2 >> 16) & ma3;
			tmp2 = tmp2 * 10000 + (tmp2 >> 32) & ma4;
			ci += 8;
			tmp = tmp * pre[8] + tmp2;
			tmp2 = *(ll*)ci;
			int dig = 36 - __builtin_ctzll((tmp2 & ma0) ^ ma0);
			tmp2 = tmp2 << dig & ma10;
			tmp2 = tmp2 * 10 + (tmp2 >> 8) & ma2;
			tmp2 = tmp2 * 100 + (tmp2 >> 16) & ma3;
			ci += (40 - dig >> 3);
			tmp = tmp * pre[32 - dig >> 3] + tmp2;
		}
	}
	return tmp;
}

const int mod = 1e9 + 7;
int to[400001], ne[400001], he[200001], Q[200001], q;
ll w[200001], W[200001];
ll kazu[20];
const ll mk = 1ll << 40 | 1 << 20 | 1, mk2 = (1ll << 20) - 1;

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);


	int N = getint();
	for(int i = 2; i < 2 * N; i++) {
		int u = getint(), v = getint();
		w[i >> 1] = getll_karini_sakusei();
		to[i] = v;
		ne[i] = he[u];
		he[u] = i++;
		to[i] = u;
		ne[i] = he[v];
		he[v] = i;
	}

	Q[q++] = 1;
	rep(p, N) {
		for (int t = he[Q[p]]; t; t = ne[t]) {
			int u = to[t];
			if (he[u] >= 0) {
				W[u] = W[Q[p]] ^ w[t >> 1];
				Q[q++] = u;
			}
		}
		he[Q[p]] = -1;
	}

	ll kotae = 0;
	rep1(i, N) {
		rep(j, 20) kazu[j] += W[i] >> j & mk;
	}
	rep(j, 20) rep(k, 3) {
		ll tmp = (kazu[j] >> k * 20 & mk2);
		kotae += (1ll << k * 20 + j) % mod * tmp % mod * (N - tmp) % mod;
	}

	co(kotae % mod);

	Would you please return 0;
}