#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define V vector<long long>
#define VV vector<vector<long long>>
#define VVV vector<vector<vector<long long>>>
#define P pair<ll,ll>
#define rep(i,n) for(ll (i)=0;(i)<(n);++(i))
#define per(i,n) for(ll (i)=(n)-1;(i)>=0;--(i))
#ifdef LOCAL
#define debug(x) cerr<<__LINE__<<": "<<#x<<"="<<x<<endl;
#define debugALL(x) cerr<<__LINE__<<": "<<#x<<"=";for(auto i=(x).begin();i!=--(x).end();i++)cerr<<*i<<" ";cerr<<*--(x).end()<<endl;
#else
#define debug(x) true
#define debugALL(x) true
#endif
using namespace std;

long long mod=1e9+7;
struct mint{
    long long x;
    mint(long long x=0):x((x%mod+mod)%mod){}
    mint operator-() const{return mint(-x);}
    mint& operator+=(const mint a){
        if((x+=a.x)>=mod)x-=mod;
        return *this;
    }
    mint& operator-=(const mint a){
        if((x+=mod-a.x)>=mod)x-=mod;
        return *this;
    }
    mint& operator*=(const mint a){
        (x*=a.x)%=mod;
        return *this;
    }
    mint operator+(const mint a)const{return mint(*this)+=a;}
    mint operator-(const mint a)const{return mint(*this)-=a;}
    mint operator*(const mint a)const{return mint(*this)*=a;}
    mint pow(long long t)const{
        if(!t)return 1;
        mint a=pow(t>>1);
        a*=a;
        if(t&1)a*=*this;
        return a;
    }
    mint inv()const{return pow(mod-2);}
    mint& operator/=(const mint a){return *this*=a.inv();}
    mint operator/(const mint a)const{return mint(*this)/=a;}
};
istream& operator>>(istream& is,mint& a){return is>>a.x;}
ostream& operator<<(ostream& os,const mint& a){return os<<a.x;}

int main(){
    ll n;
    cin>>n;
    vector<vector<P>> g(n,vector<P>());
    rep(i,n-1){
        ll u,v,w;
        cin>>u>>v>>w;
        u--;v--;
        g[u].emplace_back(v,w);
        g[v].emplace_back(u,w);
    }
    stack<P> sta;
    sta.emplace(0,0);
    vector<bool> used(n,false);
    used[0]=true;
    V sum(n);
    while(sta.size()){
        ll now=sta.top().first;
        ll w=sta.top().second;
        sta.pop();
        sum[now]=w;
        for(auto next:g[now]){
            if(used[next.first])continue;
            used[next.first]=true;
            sta.emplace(next.first,next.second^w);
        }
    }
    V cnt(60,0);
    rep(i,n){
        rep(k,60){
            if(sum[i]&1ll<<k)cnt[k]++;
        }
    }
    V ans(60,0);
    rep(i,n){
        rep(k,60){
            if(sum[i]&1ll<<k){
                ans[k]+=n-cnt[k];
            }else{
                ans[k]+=cnt[k];
            }
        }
    }
    mint tmp=0;
    rep(i,60){
        tmp+=mint(ans[i]/2)*mint(2).pow(i);
    }
    cout<<tmp<<endl;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define FOR(i, m, n) for (ll i = m; i < n; i++)
#define IFOR(i, m, n) for (ll i = n - 1; i >= m; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#define FOREACH(x, a) for (auto&(x) : (a))
#define ALL(v) (v).begin(), (v).end()
#define SZ(x) ll(x.size())

int main() {
  ll n, m;
  cin >> n >> m;
  vector<vector<P>> cond(n + 1);
  REP(i, m) {
    ll x, y, z;
    cin >> x >> y >> z;
    cond[x].push_back({y, z});
  }
  ll N = 1LL << n;
  vector<ll> dp(N, 0);
  dp[0] = 1;
  REP(bit, N) if (dp[bit] > 0) {
    vector<ll> a;
    REP(i, n) {
      if (bit & (1LL << i)) {
        a.push_back(i + 1);
      }
    }
    REP(i, n) {
      if (bit & (1LL << i)) {
        continue;
      }
      bool ok = true;
      for (P p : cond[SZ(a) + 1]) {
        ll y = p.first, z = p.second;
        ll cnt = 0;
        for (ll x : a) {
          if (x <= y) {
            cnt++;
          }
        }
        if (i + 1 <= y) {
          cnt++;
        }
        if (cnt > z) {
          ok = false;
          break;
        }
      }
      if (ok) {
        ll nxt = bit | (1LL << i);
        dp[nxt] += dp[bit];
      }
    }
  }

  cout << dp[N - 1] << endl;
}