#include <iostream>
#include <vector>

using namespace std;
using LL = long long;

const int kMaxN = 2e5 + 1;
const int kMaxM = 101;
const int kM = 1e9 + 7;

LL n, ans, u, v, w;
LL c[kMaxN], d[kMaxM];

bool vis[kMaxN];

vector<LL> a[kMaxN], b[kMaxN];

void Dfs(LL x, LL y) {
  if (vis[x]) {
    return;
  }
  vis[x] = true;
  c[x] = y;
  for (LL i = 0; i < a[x].size(); i++) {
    Dfs(a[x][i], y ^ b[x][i]);
  }
}

int main() {
  cin >> n;
  for (LL i = 1; i < n; i++) {
    cin >> u >> v >> w;
    a[u].push_back(v), b[u].push_back(w);
    a[v].push_back(u), b[v].push_back(w);
  }
  Dfs(1, 0);
  for (LL i = 1; i <= n; i++) {
    for (LL j = 0; j <= 61; j++) {
      if (c[i] & (1ll << j)) {
        d[j]++;
      }
    }
  }
  for (LL i = 0; i <= 61; i++) {
    ans = (ans + 1ll * (n - d[i]) * d[i] % kM * ((1ll << i) % kM) % kM) % kM;
  }
  cout << ans;
  return 0;
}
#include "bits/stdc++.h"

using namespace std;

using ll = int64_t;

#define eb emplace_back

#define F first
#define S second

#define ice(i, a, b) for (int (i) = (a); (i) < (b); ++(i))

const ll MOD = 1e9 + 7;

vector<vector<pair<int, ll>>> adj(200001);
vector<ll> val(200001, 0);

void dfs(int n, int m = -1) {
    for (auto b : adj[n]) {
        if (b.F != m) {
            val[b.F] = val[n] ^ b.S;
            dfs(b.F, n);
        }
    }
}

void solve() {
    int n; cin >> n;
    ice (i, 0, n - 1) {
        int u, v; ll w; cin >> u >> v >> w; --u; --v;
        adj[u].eb(v, w);
        adj[v].eb(u, w);
    }
    dfs(0);
    ll ans = 0;
    ice (i, 0, 60) {
        ll zero = 0, one = 0;
        ice (j, 0, n) {
            val[j] & (1LL << i) ? one++ : zero++;
        }
        ll cnt = (((zero * one) % MOD) * ((1LL << i) % MOD)) % MOD;
        ans = (ans + cnt) % MOD;
    }
    cout << ans;
}

int main() {
    solve();
}