You are given a tree (a connected undirected graph without cycles) of n vertices. Each of the n - 1 edges of the tree is colored in either black or red.

You are also given an integer k. Consider sequences of k vertices. Let's call a sequence [a_1, a_2, …, a_k] good if it satisfies the following criterion:

  * We will walk a path (possibly visiting same edge/vertex multiple times) on the tree, starting from a_1 and ending at a_k. 
  * Start at a_1, then go to a_2 using the shortest path between a_1 and a_2, then go to a_3 in a similar way, and so on, until you travel the shortest path between a_{k-1} and a_k.
  * If you walked over at least one black edge during this process, then the sequence is good. 

<image>

Consider the tree on the picture. If k=3 then the following sequences are good: [1, 4, 7], [5, 5, 3] and [2, 3, 7]. The following sequences are not good: [1, 4, 6], [5, 5, 5], [3, 7, 3].

There are n^k sequences of vertices, count how many of them are good. Since this number can be quite large, print it modulo 10^9+7.

Input

The first line contains two integers n and k (2 ≤ n ≤ 10^5, 2 ≤ k ≤ 100), the size of the tree and the length of the vertex sequence.

Each of the next n - 1 lines contains three integers u_i, v_i and x_i (1 ≤ u_i, v_i ≤ n, x_i ∈ \{0, 1\}), where u_i and v_i denote the endpoints of the corresponding edge and x_i is the color of this edge (0 denotes red edge and 1 denotes black edge).

Output

Print the number of good sequences modulo 10^9 + 7.

Examples

Input


4 4
1 2 1
2 3 1
3 4 1


Output


252

Input


4 6
1 2 0
1 3 0
1 4 0


Output


0

Input


3 5
1 2 1
2 3 0


Output


210

Note

In the first example, all sequences (4^4) of length 4 except the following are good: 

  * [1, 1, 1, 1]
  * [2, 2, 2, 2]
  * [3, 3, 3, 3]
  * [4, 4, 4, 4] 



In the second example, all edges are red, hence there aren't any good sequences.
#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
long long int INF = 1001001001001001LL;
int inf = 1000000007;
long long int MOD = 1000000007LL;
double PI = 3.1415926535897932;
long long int di[] = {-1, 0, 1, 0, -1, 1, 1, -1};
long long int dj[] = {0, 1, 0, -1, 1, 1, -1, -1};
template <typename T1, typename T2>
inline void chmin(T1 &a, const T2 &b) {
  if (a > b) a = b;
}
template <typename T1, typename T2>
inline void chmax(T1 &a, const T2 &b) {
  if (a < b) a = b;
}
struct UnionFind {
  vector<int> M_par;
  vector<int> M_rank;
  vector<int> M_size;

 private:
  void init(int n = 1) {
    M_par.resize(n);
    M_rank.resize(n);
    M_size.resize(n);
    for (int i = 0; i < n; i++) {
      M_par[i] = i;
      M_rank[i] = 0;
      M_size[i] = 1;
    }
  }

 public:
  UnionFind(size_t n = 1) { init(n); }
  size_t find(size_t x) {
    if (M_par[x] == x) return x;
    size_t r = find(M_par[x]);
    return M_par[x] = r;
  }
  bool same(size_t x, size_t y) { return find(x) == find(y); }
  bool unite(size_t x, size_t y) {
    x = find(x);
    y = find(y);
    if (x == y) return false;
    if (M_rank[x] < M_rank[y]) swap(x, y);
    if (M_rank[x] == M_rank[y]) M_rank[x]++;
    M_par[y] = x;
    M_size[x] = M_size[x] + M_size[y];
    return true;
  }
  size_t size(size_t x) { return M_size[find(x)]; }
};
const long long MAXN = 1001024;
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long extgcd(long long a, long long b, long long &x, long long &y) {
  long long d = a;
  if (b != 0) {
    d = extgcd(b, a, y, x);
    y -= (a / b) * x;
  } else {
    x = 1;
    y = 0;
  }
  return d;
}
long long prime[MAXN];
bool is_prime[MAXN];
bool is_prime_ab[MAXN];
long long sieve(long long n) {
  long long res = 0;
  fill(is_prime, is_prime + MAXN, true);
  is_prime[0] = is_prime[1] = false;
  for (long long i = 2; i <= n; ++i) {
    if (!is_prime[i]) continue;
    prime[res++] = i;
    for (long long j = 2 * i; j <= n; j += i) is_prime[j] = false;
  }
  return res;
}
vector<long long> divisors(long long n) {
  vector<long long> res;
  for (long long i = 1; i * i <= n; ++i) {
    if (n % i != 0) continue;
    res.push_back(i);
    if (n / i == i) continue;
    res.push_back(n / i);
  }
  return res;
}
map<long long, long long> prime_factors(long long n) {
  map<long long, long long> res;
  if (n == 1) {
    res[n] = 1;
    return res;
  }
  for (long long i = 2, _n = n; i * i <= _n; ++i) {
    while (n % i == 0) {
      ++res[i];
      n /= i;
    }
  }
  if (n != 1) res[n] = 1;
  return res;
}
long long modpow(long long a, long long n) {
  long long res = 1;
  while (n > 0) {
    if (n & 1) res = res * a % MOD;
    a = a * a % MOD;
    n >>= 1;
  }
  return res;
}
long long modinv(long long a, long long m) {
  long long b = m, u = 1, v = 0;
  while (b) {
    long long t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0) u += m;
  return u;
}
long long fac[MAXN], finv[MAXN], inv[MAXN];
void math_init() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (long long i = 2; i < MAXN; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}
long long COM(long long n, long long k) {
  if (n < k) return 0;
  if (n < 0 || k < 0) return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
int main() {
  int n, k;
  cin >> n >> k;
  UnionFind uf(n);
  for (int i = 0; i < n - 1; i++) {
    int a, b, x;
    cin >> a >> b >> x;
    a--;
    b--;
    if (x == 0) uf.unite(a, b);
  }
  lint ans = modpow(n, k);
  for (int i = 0; i < n; i++) {
    if (uf.find(i) == i) {
      ans = ans + MOD - modpow(uf.size(i), k);
      ans %= MOD;
    }
  }
  cout << ans << endl;
  return 0;
}
