func_code_string: #include <bits/stdc++.h>   using namespace std;   const long long MOD = 1000000007ll;   const int INTMAX = numeric_limits<int>::max();   const long long LLMAX = numeric_limits<long long>::max();   void writeln() { cout <<   n ; }   template <class T>   inline void print(T&& a);   template <class T>   inline void priws(T&& a);   template <class T>   inline void read(T& a);   template <class... Args>   inline void readln(Args&... args) {     (read(args), ...);   }   template <class H, class... T>   inline void writeln(H&& h, T&&... t) {     priws(h);     (print(t), ...);     writeln();   }   template <class T>   inline void writeln_range(T f, T s) {     if (f != s)       for (auto i = f; i != s; ++i) writeln(*i);   }   vector<string> split(string& s, string d) {     vector<string> v;     size_t p = 0;     while ((p = s.find(d)) != string::npos)       v.push_back(s.substr(0, p)), s.erase(0, p + d.length());     v.push_back(s);     return v;   }   template <class... Args>   inline void err(string v, Args... args) {     auto vv = split(v,  ,  );     auto it = vv.begin();     (writeln(*it++,  = , args), ...);   }   template <typename T>   T binpowmod(T a, long long n, T mod) {     assert(n > 0);     T res = a;     --n;     while (n > 0) {       if (n & 1) res = (res * a) % mod;       a = (a * a) % mod;       n >>= 1;     }     return res;   }   void run() {     int n, k;     readln(n, k);     vector<vector<pair<int, int>>> g(n);     int u, v, x;     for (int i = 0; i < (int)(n - 1); ++i)       readln(u, v, x), --u, --v, g[u].push_back({v, x}), g[v].push_back({u, x});     vector<long long> degs(n + 1, 0);     for (int i = 1; i < (int)(n + 1); ++i) degs[i] = binpowmod(i * 1ll, k, MOD);     vector<bool> used(n, false);     function<int(int)> dfs = [&](int u) {       used[u] = true;       int cnt = 1;       for (auto& [v, x] : g[u])         if (!x && !used[v]) cnt += dfs(v);       return cnt;     };     long long ans = degs[n];     for (int i = 0; i < (int)(n); ++i)       if (!used[i]) {         int cnt = dfs(i);         ans -= degs[cnt];         if (ans < 0) ans += MOD;       }     writeln(ans);   }   int main() {     ios_base::sync_with_stdio(false);     run();     return 0;   }   template <class T>   inline ostream& operator<<(ostream& os, vector<T> const& _a);   template <class T1, class T2>   inline istream& operator>>(istream& is, pair<T1, T2>& _a) {     return is >> _a.first >> _a.second;   }   template <class T1, class T2>   inline ostream& operator<<(ostream& os, pair<T1, T2> const& _a) {     return os << _a.first <<     << _a.second;   }   template <class T>   inline ostream& operator<<(ostream& os, vector<T> const& _a) {     if (_a.size())       os << _a[0];     else       os <<   n ;     for (int i = 1; i < (int)(_a.size()); ++i)       os <<   n  [is_fundamental<T>::value] << _a[i];     return os;   }   template <class T>   inline ostream& operator<<(ostream& os, valarray<T> const& _a) {     if (_a.size())       os << _a[0];     else       os <<   n ;     for (int i = 1; i < (int)(_a.size()); ++i)       os <<   n  [is_fundamental<T>::value] << _a[i];     return os;   }   template <class T>   inline istream& operator>>(istream& is, vector<T>& _a) {     for (int i = 0; i < (int)(_a.size()); ++i) is >> _a[i];     return is;   }   template <class T>   inline istream& operator>>(istream& is, valarray<T>& _a) {     for (int i = 0; i < (int)(_a.size()); ++i) is >> _a[i];     return is;   }   template <class T>   inline void print(T&& _a) {     cout <<     << _a;   }   template <class T>   inline void priws(T&& _a) {     cout << _a;   }   template <class T>   inline void read(T& _a) {     cin >> _a;   }   