/*
{
######################
#       Author       #
#        Gary        #
#        2021        #
######################
*/
#include<bits/stdc++.h>
#define rb(a,b,c) for(int a=b;a<=c;++a)
#define rl(a,b,c) for(int a=b;a>=c;--a)
#define LL long long
#define IT iterator
#define PB push_back
#define II(a,b) make_pair(a,b)
#define FIR first
#define SEC second
#define FREO freopen("check.out","w",stdout)
#define rep(a,b) for(int a=0;a<b;++a)
#define SRAND mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define random(a) rng()%a
#define ALL(a) a.begin(),a.end()
#define POB pop_back
#define ff fflush(stdout)
#define fastio ios::sync_with_stdio(false)
#define check_min(a,b) a=min(a,b)
#define check_max(a,b) a=max(a,b)
using namespace std;
inline int read(){
    int x=0;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        x=(x<<1)+(x<<3)+(ch^48);
        ch=getchar();
    }
    return x;
}
const int INF=0x3f3f3f3f;
typedef pair<int,int> mp;
/*}
*/
const int MAXN=2e5+233;
vector<pair<int,LL> > g[MAXN];
int n,depth[MAXN];
const int MOD=1e9+7;
LL ans=0;
int t;
int cnt[2];
void solve(int now,int pre){
	for(auto it:g[now]) if(it.FIR!=pre){
		depth[it.FIR]=depth[now]^((it.SEC>>t)&1);
		solve(it.FIR,now);
	}
	cnt[depth[now]]++;
}
int main(){
//	freopen("test.txt","r",stdin);
	scanf("%d",&n);
	rb(i,1,n-1){
		int u,v;
		LL w;
		u=read();
		v=read();
		scanf("%lld",&w);
		g[u].PB(II(v,w));
		g[v].PB(II(u,w));
	}
	rep(i,60){
		cnt[0]=cnt[1]=0;
		t=i;
		solve(1,0);
		LL cnt_=1ll*cnt[0]*cnt[1]%MOD;
		ans+=(1ll<<t)%MOD*cnt_%MOD; 
//		cout<<cnt_<<endl;
		ans%=MOD;
	}
	cout<<ans<<endl;
	return 0;
}

#pragma GCC optimize("O3")
#pragma GCC target("sse4")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_multi_set;

#define mod         1000000007
#define pi          3.1415926535898
#define eps         1e-9


#define fast       ios::sync_with_stdio(0); cin.tie(0);cout.tie(0)
#define vt vector
#define ar array
#define fs          first
#define sc          second
#define pb         push_back
#define sp          printf(" ")
#define nl          '\n'
#define all(a) a.begin(),a.end()
#define unique(c) (c).resize(unique(all(c)) - (c).begin())
#define sz(x) (int)(x).size()
#define revsort(x) sort(all(x));reverse(all(x))

#define set0(a)     memset(a,0,sizeof(a))
#define setneg(a)   memset(a,-1,sizeof(a))
#define setinf(a)   memset(a,126,sizeof(a))

#define REP(i,start,end) for (int i = start; i <=end; i++)
#define RREP(i,end,start) for (int i=end; i>=start ;i--)
#define EACH(x, a) for (auto& x: a)

typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
typedef pair<double, double> pdd;
typedef vector<int> vi;
typedef vector<LL> vll;
typedef vector<double> vd;
typedef vector<vector<LL> > matrix;
typedef vector<vector<pii> > graph;


 
template<class A> void read(vt<A>& v);
template<class A, size_t S> void read(ar<A, S>& a);
template<class T> void read(T& x) {
    cin >> x;
}
void read(double& d) {
    string t;
    read(t);
    d=stod(t);
}
void read(long double& d) {
    string t;
    read(t);
    d=stold(t);
}
template<class H, class... T> void read(H& h, T&... t) {
    read(h);
    read(t...);
}
template<class A> void read(vt<A>& x) {
    EACH(a, x)
        read(a);
}
template<class A, size_t S> void read(array<A, S>& x) {
    EACH(a, x)
        read(a);
}

LL powm(LL x, LL y,LL m) {
    if (y==0) return 1;
    if (y==1) return x%m;
    if (y&1) return (x*(powm((x*x)%m,y/2,m)))%m;
    return powm((x*x)%m,y/2,m)%m;
}

LL inv(LL x, LL m) {
    return powm(x,m-2,m);
}

int add(int var1, int var2) {
    return (((((LL)(var1)) % mod + (LL)(var2)) % mod) + mod) % mod;
}


int mul(int var1, int var2) {
    return (((LL)(var1)) % mod * (LL)(var2)) % mod;
}

LL INF=1e9;

graph adj;
int n, m;
vector<bool> visited;
vector<vector<int>> ex;

vi ans;
void dfs(int i) {
    visited[i]=1;
    for(auto tx: adj[i]) {
        if(!visited[tx.fs]) {
            if(tx.sc==ans[i]) {
                ex[tx.fs].pb(ans[i]);
            } else {
                ans[tx.fs] = tx.sc;
                ex[i].pb(tx.sc);
            }
            dfs(tx.fs);
        }
    }
}

void solve() {
    read(n,m);
    adj.resize(n);
    ans.assign(n,-1);
    ex.resize(n);
    visited.assign(n,0);


    REP(i,1,m) {

        int x,y,z;
        read(x,y,z);
        x--;
        y--;
        adj[x].pb({y,z});
        adj[y].pb({x,z});

    }
    ans[0]=1;
    dfs(0);
    REP(i,0,n-1) {
        if(ans[i]==-1) {
            int ix=1;
            sort(all(ex[i]));
            REP(j,0,sz(ex[i])) {
                if(ix==ex[i][j]) {
                    ix++;
                    continue;
                }
                break;
            }
            ans[i]=ix;
        }
    }
    EACH(x,ans) cout<<x<<nl;



}

int main() {
    fast;
        solve();
}
