#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <queue>
#include <stack>
using namespace std;
typedef long long ll;
const ll mod = 1000 * 1000 * 1000 + 7;

vector<ll> v; ll res = 0;

struct edge {
	int u, v; ll w; edge() {}
	edge(int _u, int _v, ll _w) :
		u(_u), v(_v), w(_w) {}
};

struct node { vector<edge> edges; };

struct graph {
	vector<node> nodes; int n;
	graph(int _n) : n(_n) { nodes.resize(n); }

	void add_edge(int u, int v, ll w) {
		nodes[u].edges.emplace_back(u, v, w);
		nodes[v].edges.emplace_back(v, u, w);
	}

	void dfs1(int cur, int prev, ll W) {
		v[cur] = W; for (edge& e : nodes[cur].edges)
			if (e.v != prev) { dfs1(e.v, cur, W ^ e.w); }
	}

	void dfs2(int cur, int prev, int cnt, ll W, bool inv = false) {
		ll cont = ((inv ? n - cnt : cnt) * (W % mod)) % mod;
		res = (res + cont) % mod; for (edge& e : nodes[cur].edges) {
			if (e.v == prev) { continue; }
			bool b = (e.w & W) != 0;
			dfs2(e.v, cur, cnt, W, inv ^ b);
		}
	}
};

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

	int n; cin >> n; graph g(n); v.resize(n);
	for (int i = 1; i < n; i++) {
		int u, v; ll w; cin >> u >> v >> w;
		g.add_edge(--u, --v, w);
	}

	g.dfs1(0, -1, 0);

	for (int i = 0; i <= 60; i++) {
		int cnt = 0; ll W = 1ll << i;
		for (int j = 0; j < n; j++)
			if (v[j] & W) { cnt++; }
		g.dfs2(0, -1, cnt, W);
	}

	cout << (res * 500000004) % mod << '\n';
	cin.ignore(2); return 0;
}
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define eb emplace_back
#define x first
#define y second
#define FOR(i, m, n) for (ll i(m); i < n; i++)
#define DWN(i, m, n) for (ll i(m); i >= n; i--)
#define REP(i, n) FOR(i, 0, n)
#define DW(i, n) DWN(i, n, 0)
#define F(n) REP(i, n)
#define FF(n) REP(j, n)
#define D(n) DW(i, n)
#define DD(n) DW(j, n)
using ll = long long;
using ld = long double;
using pll = pair<ll, ll>;
using tll = tuple<ll, ll, ll>;
using vll = vector<ll>;
using vpll = vector<pll>;
using vtll = vector<tll>;
using gr = vector<vll>;
using wgr = vector<vpll>;
void add_edge(gr&g,ll x, ll y){ g[x].pb(y);g[y].pb(x); }
void add_edge(wgr&g,ll x, ll y, ll z){ g[x].eb(y,z);g[y].eb(x,z); }
template<typename T,typename U>
ostream& operator<<(ostream& os, const pair<T,U>& p) { 
    cerr << ' ' <<  p.x << ',' << p.y; return os; }
template <typename T> 
ostream& operator<<(ostream& os, const vector<T>& v) { 
    for(auto x: v) os << ' ' <<  x; return os; }
template <typename T> 
ostream& operator<<(ostream& os, const set<T>& v) { 
    for(auto x: v) os << ' ' <<  x; return os; }
template<typename T,typename U>
ostream& operator<<(ostream& os, const map<T,U>& v) { 
    for(auto x: v) os << ' ' <<  x; return os; }
struct d_ {
    template<typename T> d_& operator,(const T& x) {
        cerr << ' ' <<  x; return *this;}
} d_t;
#define dbg(args ...) { d_t,"|",__LINE__,"|",":",args,"\n"; }
#define deb(X ...) dbg(#X, "=", X);
#define EPS (1e-10)
#define INF (1LL<<61)
#define YES(x) cout << (x ? "YES" : "NO") << endl;
#define CL(A,I) (memset(A,I,sizeof(A)))
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()

struct E {
    ll x,y,w;
};

const ll MOD = 1e9+7;
ll mod(ll a, ll b=MOD) {
  return ((a%b)+b)%b;
}
ll extended_euclid(ll a,ll b,ll &x,ll &y) {
    if (a == 0) { x = 0; y = 1; return b;}
    ll x1, y1; ll d = extended_euclid(b%a, a, x1, y1);
    x = y1 - (b / a) * x1;
    y = x1;
    return d;
}
ll mod_inverse(ll a, ll n=MOD) {
  ll x, y;
  ll d = extended_euclid(a, n, x, y);
  if (d > 1) return -1;
  return mod(x,n);
}
struct Mt {
    ll x = 0;
    Mt(){}
    Mt(ll y):x(mod(y)){}
    bool operator==(Mt &y){return x==y.x;}
    Mt operator+(Mt y){return mod(x+y.x);}
    Mt operator-(){return mod(-x);}
    Mt operator-(Mt y){return mod(x-y.x);}
    Mt operator*(Mt y){return mod(x*y.x);}
    Mt operator/(Mt y){return mod(x*mod_inverse(y.x));}
    Mt operator+=(Mt y){return x=(*this+y).x;}
    Mt operator-=(Mt y){return x=(*this-y).x;}
    Mt operator*=(Mt y){return x=(*this*y).x;}
    Mt operator/=(Mt y){return x=(*this/y).x;}
};
ostream& operator<<(ostream& os, const Mt&m){
    os << m.x; return os;
}


ll cnt[67];
void dfs(ll u, wgr &g, ll p=-1, ll on=0) {
    F(60) cnt[i]+=(on&(1ll<<i))?1:0;
    for(auto [v,w]: g[u]) if(v!=p) dfs(v,g,u, on^w);
}

int main(void) {
    ios_base::sync_with_stdio(false);
    ll n; cin >> n;
    wgr g(n);
    F(n-1) {
        ll u,v,w; cin >> u >> v >> w;
        u--; v--;
        add_edge(g, u,v,w);
    }
    dfs(0,g);
    ll ret = 0;
    F(60) {
        ll cur = cnt[i];
        ret = mod(ret + mod(1ll<<i) * mod((cur) * (n-cur)));
    }
    cout << ret << endl;
    return 0;
}
