#include <bits/stdc++.h>
#define FOR(ii,aa,bb) for(int ii=aa;ii<bb;ii++)
#define for0(ii,bb) FOR(ii,0,bb)
#define for1(ii,bb) FOR(ii,1,bb+1)
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define st first
#define nd second
#define pii pair<int,int>
#define piii pair<int,pii>
#define pdi pair<double,int>
#define vi vector<int>
#define sp " "
#define nl "\n"
#define all(x) x.begin(),x.end()
#define fastio() ios_base::sync_with_stdio(0);cin.tie(0);
#define ll long long
#define int ll
 
using namespace std;
 
const int N = 200005;
const int INF = 1e9+5;
const int mod = 1e9+7;

int n,ans;
vector<vector<pii>> g;
vector<int> dist;

int topla(int x,int y){
    return (x+y)%mod;
}

int carp(int x,int y){
    return x*y%mod;
}

void dfs(int u,int d,int p=-1){
    dist[u]=d;
    for(auto e:g[u]){
        int v=e.st,w=e.nd;
        if(v==p)continue;
        dfs(v,d^w,u);
    }
}

signed main(){
    #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    #endif
    fastio()
    cin >> n;
    g.resize(n+1);
    dist.resize(n+1);
    for(int i=1;i<n;i++){
        int u,v,w;
        cin >> u >> v >> w;
        g[u].pb({v,w});
        g[v].pb({u,w});
    }
    dfs(1,0);
    for(int i=0;i<60;i++){
        vector<int> cnt(2);
        for(int j=1;j<=n;j++){
            cnt[(dist[j]>>i)&1ll]++;
        }
        ans=topla(ans,carp(carp(cnt[0],cnt[1]),(1ll<<i)%mod));
    }
    cout << ans << nl;
}
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
using Vi = vector<int>;
using VVi = vector<Vi>;
using Vl = vector<ll>;
using VVl = vector<Vl>;
using P = pair<int, int>;
using Vp = vector<P>;
using VVp = vector<Vp>;
using pl = pair<ll, int>;
using Vpl = vector<pl>;
using VVpl = vector<Vpl>;


ll gcd(ll a, ll b)
{
    while (a) {
        b %= a;
        swap(a, b);
    }
    return b;
}

ll dsum(ll a)
{
    ll num = 0;
    while (a) {
        num += a % 10ll;
        a /= 10ll;
    }
    return num;
}


bool is_pal(string S)
{
    bool ok = true;
    for (int i = 0; i < (int)S.size(); i++) {
        int j = (int)S.size() - i - 1;
        ok = ok && (S[i] == S[j]);
    }
    return ok;
}

ll m = 998244353;
ll mpow(ll a, ll n)
{
    ll ret = 1;
    while (n) {
        if (n % 2) {
            ret *= a;
            ret %= m;
        }
        a = (a * a) % m;
        n /= 2;
    }
    return ret;
}

int calc(int a, int N)
{
    if (a > N) {
        return a - N;
    } else {
        return a + N;
    }
}
int main()
{
    int N, M;
    cin >> N >> M;
    VVp cond(N + 1);
    for (int i = 0; i < M; i++) {
        int x, y, z;
        cin >> x >> y >> z;
        cond[x].push_back(make_pair(y, z));
    }
    ll dp[1 << 19];
    dp[0] = 1;
    for (int i = 1; i < (1 << N); i++) {
        Vi num(N + 1, 0);
        dp[i] = 0;
        int di = 0;
        for (int j = 0; j < N; j++) {
            if ((i >> j) & 1) {
                dp[i] += dp[i - (1 << j)];
                num[j + 1]++;
                di++;
            }
        }
        for (int j = 1; j <= N; j++) {
            num[j] += num[j - 1];
        }
        bool ok = true;
        for (P con : cond[di]) {
            int y, z;
            tie(y, z) = con;
            if (num[y] > z) {
                ok = false;
            }
        }
        if (!ok) {
            dp[i] = 0;
        }
    }
    cout << dp[(1 << N) - 1] << endl;
}
