#include<bits/stdc++.h>
using namespace std;
#define rep(i,x,y) for(int i=(int)(x);i<(int)(y);i++)
#define print(A,x,n) rep(i,0,n){cout<<(i ? " ":"")<<A[i]x;}cout<<endl;
#define pprint(A,y,m,n) rep(j,0,m){print(A[j],y,n);}
const long mod=1e9+7;
const int siz=2e5;
const int inf=1e9;
vector<pair<int,long>> g[siz];
long dist[siz];
void dfs(int v, int par, long d){
	dist[v] = d;
	for(pair<int,long> p:g[v]){
		int u = p.first;
		long w = p.second;
		if(u == par) continue;
		dfs(u, v, d ^ w);
	}
}
long calc(long x, int j){
  	rep(i,0,j) (x *= 2) %= mod;
  	return x;
}
int main(){
	int N; cin>>N;
	int a,b; long c;
	rep(i,0,N-1){
		cin>>a>>b>>c; a--; b--;
		g[a].push_back({b, c});
		g[b].push_back({a, c});
	}
	dfs(0, -1, 0);
	long cnt[60] = {};
	rep(i,0,N)rep(j,0,60){
		if(dist[i] & (1L<<j)) cnt[j]++;
	}
	long ans = 0;
	rep(j,0,60) (ans += calc(cnt[j] * (N-cnt[j]) % mod, j)) %= mod;
	cout<<ans<<endl;
}
//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,fma")
//#pragma GCC optimization ("unroll-loops")

#include <bits/stdc++.h>
using namespace std;
using namespace chrono;

#define ll long long
#define ld long double
#define mod 1000000007
#define pb push_back
#define inf 1000000000000000000
#define ff first
#define ss second
#define deb(x) cout<<#x<<" "<<x<<"\n"
#define Clear(x) memset(x,0,sizeof(x))
#define all(x) (x).begin(),(x).end()

void checkpoint1()
{
    /******think more code less******/
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    #ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
        freopen("Error.txt", "w", stderr);
    #endif
}

bool comp(pair< ll,ll > &a,pair< ll,ll > &b)
{
    if(a.ff!=b.ff)
        return a.ff<b.ff;
    else
    {
        if(a.ss>b.ss)
            return b.ss<a.ss;
        else if(a.ss<b.ss)
            return a.ss<b.ss;
    }
    return a.ff>b.ff;
}

ll powe(ll a,ll b)
{
    ll res=1;
    while(b>0)
    {
        if(b&1)
        {
            res*=a;
            res%=mod;
        }
        a*=a;
        a%=mod;
        b>>=1;
    }
    return res;
}

void terminAtor()
{
    ll n;
    cin>>n;
    vector< pair< ll,ll > > adj[n+1];
    for(ll i=1;i<n;i++)
    {
        ll x,y,z;
        cin>>x>>y>>z;
        adj[x].pb({z,y});
        adj[y].pb({z,x});
    }
    vector< bool > vis(n+1,0);
    vector< ll > dis(n+1,0);
    queue< ll > q;
    q.push(1);
    while(!q.empty())
    {
        ll v=q.front();
        q.pop();
        vis[v]=1;
        for(auto u:adj[v])
        {
            if(!vis[u.ss])
            {
                q.push(u.ss);
                dis[u.ss]=dis[v]^u.ff;
            }
        }
    }
    ll ans=0;
    for(ll i=0;i<60;i++){
        ll tot0=0,tot1=0;
        for(ll ii=1;ii<=n;ii++)
        {
            if(dis[ii]&(1LL<<i))
                tot1++;
            else
                tot0++;
        }
        tot1*=tot0;
        tot1%=mod;
        ll ok=(1LL<<i);
        ok%=mod;
        tot1*=(ok);
        tot1%=mod;
        ans+=tot1;
        ans%=mod;
    }
    cout<<ans;
}

int main()
{    
    checkpoint1();
/********************************************************/    
    auto startrrr = high_resolution_clock::now();
    cout << setprecision(20);
/*******************************************************/    
    
    terminAtor();


/*******************************************************/    
    auto stoprrr = high_resolution_clock::now();
    auto duration = duration_cast<microseconds>(stoprrr - startrrr);
    #ifndef ONLINE_JUDGE
        cerr << "Time: " << duration.count()/1000.0<<"\n";
    #endif
/*******************************************************/        
    return 0;
}