#include "bits/stdc++.h"
// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,mmx,avx,avx2")
using namespace std;
using ll = long long;

const int mod = 1e9+7;
ll ans = 0;

array<int, 61> dfs(auto &g, int u, int par, auto &pref, auto &sz)
{
    sz[u] = 1;
    array<int, 61> ret{};
    int tot = 0;
    for (auto [v, w] : g[u]) {
        if (v == par) continue;
        pref[v] = pref[u] ^ w;
        auto child = dfs(g, v, u, pref, sz);
        sz[u] += sz[v];
        for (int i = 0; i < 61; ++i) {
            ll mul = 1LL << i; mul %= mod;
            ll ct = 1LL*child[i]*(tot-ret[i]) + 1LL*(sz[v]-child[i])*ret[i];
            ct %= mod;
            ans += mul*ct; ans %= mod;
            ret[i] += child[i];
        }
        tot += sz[v];
    }
    for (int i = 0; i < 61; ++i) {
        ll mul = 1LL << i; mul %= mod;
        ll ct = 0;
        if ((pref[u]>>i)&1) {
            ct = sz[u]-1-ret[i];
            ++ret[i];
        }
        else {
            ct = ret[i];
        }
        ct %= mod;
        ans += mul*ct; ans %= mod;
    }
    return ret;
}

int main()
{
    ios::sync_with_stdio(0); cin.tie(0);
    mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

    int n; cin >> n;
    vector<vector<array<ll, 2>>> g(n);
    for (int i = 0; i < n-1; ++i) {
        int u, v; cin >> u >> v;
        ll w; cin >> w;
        g[--u].push_back({--v, w});
        g[v].push_back({u, w});
    }
    vector<ll> pref(n);
    vector<int> sz(n);
    dfs(g, 0, 0, pref, sz);
    cout << ans;
}
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n,m,dis[1001001],mod=1e9+7,u,v,w,num,ans;
struct node
{
	int v,w;
}t;
vector<node>g[1001001];
void dfs(int u,int fa)
{
	for(int i=0;i<g[u].size();i++)
	{
		int v=g[u][i].v;
		if(g[u][i].v==fa)
			continue;
		dis[v]=dis[u]^g[u][i].w;
		dfs(v,u);
	}
}
signed main()
{
	scanf("%lld",&n);
	for(int i=1;i<+n;i++)
	{
		scanf("%lld%lld%lld",&u,&v,&w);
		t.v=v;
		t.w=w;
		g[u].push_back(t);
		t.v=u;
		g[v].push_back(t);
	}
	dfs(1,0);
	for(int j=60;j>=0;j--)
	{
		num=0;
		for(int i=1;i<=n;i++)
			if((dis[i]&(1ll<<j))>0)
				num++;
		ans=(ans+(((1ll<<j)%mod)*num%mod)*(n-num)%mod)%mod;
	}
	cout<<ans;
    return 0;
}