#include using namespace std; #define int long long struct dsu { vector fa, sz; dsu(int n) : fa(n), sz(n, 1){iota(fa.begin(), fa.end(), 0);} int find(int x){while (x != fa[x]) x = fa[x] = fa[fa[x]]; return x;} bool same(int x, int y){return find(x) ==find(y);} bool merge(int x, int y) { x = find(x); y = find(y); if (x == y) return false; sz[x] += sz[y]; fa[y] = x; return true; } int size(int x){return sz[find(x)];} }; constexpr int P = 1000000007; // assume -P <= x < 2P int norm(int x) { if (x < 0) x += P; if (x >= P) x -= P; return x; } template T qpow(T a, int b) { T res = 1; for (; b; b /= 2, a *= a) { if (b % 2) res *= a; } return res; } struct Z { int x; Z(int x = 0) : x(norm(x)){} int val()const{return x;} Z operator - ()const{return Z(norm(P - x));} Z inv()const {assert(x != 0); return qpow(*this, P - 2);} Z &operator *= (const Z &rhs){x = x * rhs.x % P; return *this;} Z &operator += (const Z &rhs){x = norm(x + rhs.x); return *this;} Z &operator -= (const Z &rhs){x = norm(x - rhs.x); return *this;} Z &operator /= (const Z &rhs){return *this *= rhs.inv();} friend Z operator * (const Z &lhs, const Z &rhs){Z ret = lhs; ret *= rhs; return ret;} friend Z operator + (const Z &lhs, const Z &rhs){Z ret = lhs; ret += rhs; return ret;} friend Z operator - (const Z &lhs, const Z &rhs){Z ret = lhs; ret -= rhs; return ret;} friend Z operator / (const Z &lhs, const Z &rhs){Z ret = lhs; ret /= rhs; return ret;} }; signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector> g(n - 1); for (int i = 1; i < n; i ++) { int u, v, w; cin >> u >> v >> w; u --; v --; g.push_back(array{u, v, w}); } Z ans = 0; for (int i = 30; i --; ) { ans += ans; dsu d(n); for (auto [u, v, w] : g) { if (w >> i & 1) { ans += d.size(u) * d.size(v); d.merge(u, v); } } } cout << ans.val() << "\n"; return 0; }