#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1e18;
const int inf = 1e9;
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define per(i, a, b) for (int i = b - 1; i >= a; i--)
#define all(a) (a).begin(), (a).end()
using pint = pair<ll, ll>;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

ll seen[200200];
int n;
vector<vector<pint>> g(200200);

int main() {
    cin >> n;
    rep(i, 0, n - 1) {
        ll u, v, w;
        cin >> u >> v >> w;
        --u, --v;
        g[u].push_back({w, v});
        g[v].push_back({w, u});
    }
    rep(i, 0, n) seen[i] = -1;
    seen[0] = 0;
    queue<ll> q;
    q.push(0);
    while (!q.empty()) {
        int now = q.front();
        q.pop();
        for (auto p : g[now]) {
            ll next = p.second, w = p.first;
            if (seen[next] == -1) {
                seen[next] = seen[now] ^ w;
                q.push(next);
            }
        }
    }
    ll ans = 0;
    const ll mod = 1e9 + 7;
    rep(i, 0, 60) {
        ll cnt0 = 0, cnt1 = 0;
        rep(j, 0, n) {
            if (seen[j] & 1LL << i)
                cnt1++;
            else
                cnt0++;
        }
        // cout << i << " : " << cnt0 << cnt1 << "\n";
        ans = (ans + (1LL << i) % mod * cnt0 * cnt1 % mod) % mod;
    }
    cout << ans << "\n";
}
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int N = 400010, mod = 1e9 + 7;

int e[N], ne[N], h[N], idx, w[N][61], n;
ll tar, d[N][2], res;

void add(int a, int b, long long c) {
	e[idx] = b, ne[idx] = h[a];
	for (int i = 0; i < 60; ++i) {
		if (c & 1) w[idx][i] = 1;
		c /= 2;
		if (!c) break;
	}
	h[a] = idx++;
}

void go(int x, int fa) {
	d[x][0] = d[x][1] = 0;
	for (int i = h[x]; i != -1; i = ne[i]) {
		if (fa == e[i]) continue;
		go(e[i], x);
		if (w[i][tar]) {
			d[x][0] += d[e[i]][1];
			d[x][1] += d[e[i]][0] + 1;
		} else {
			d[x][0] += d[e[i]][0] + 1;
			d[x][1] += d[e[i]][1];
		}
	}	
	res += d[x][1];
	ll t = 0;
	for (int i = h[x]; i != -1; i = ne[i]) {
		if (fa == e[i]) continue;
		if (w[i][tar]) {
			res += (d[e[i]][0] + 1) * (d[x][0] - d[e[i]][1]);
		} else {
			res += d[e[i]][1] * (d[x][0] - d[e[i]][0] - 1); 
		}
	}
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> n;
	memset(h, -1, sizeof h);
	for (int i = 1; i < n; ++i) {
		int a, b;
		ll w;
		cin >> a >> b >> w;
		add(a, b, w);
		add(b, a, w);
	}
	ll ans = 0;
	for (int i = 0; i < 60; ++i) {
		tar = i;
		res = 0;
		go(1, -1);
		ans = (ans + res % mod * ((1ll << i) % mod) % mod) % mod;
	}
	cout << ans << '\n';
	return 0;
}
