1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
#ifndef LOCAL
#pragma GCC optimize ("O3")
#endif

#include <bits/stdc++.h>

using namespace std;

#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return {i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (c it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(x...) " [" #x ": " << (x) << "] "

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
template <typename A, typename B>
using unordered_map2 = __gnu_pbds::gp_hash_table<A, B>;
using namespace __gnu_pbds;
template <typename T> using ordered_set =
  __gnu_pbds::tree<T, __gnu_pbds::null_type, less<T>, __gnu_pbds::rb_tree_tag,
                   __gnu_pbds::tree_order_statistics_node_update>;
// ordered_set<int> s; s.insert(1); s.insert(2);
// s.order_of_key(1);    // Out: 0.
// *s.find_by_order(1);  // Out: 2.

using ld = long double;
using ll = long long;

constexpr int mod = 1000 * 1000 * 1000 + 7;
constexpr int odw2 = (mod + 1) / 2;

void OdejmijOd(int& a, int b) { a -= b; if (a < 0) a += mod; }
int Odejmij(int a, int b) { OdejmijOd(a, b); return a; }
void DodajDo(int& a, int b) { a += b; if (a >= mod) a -= mod; }
int Dodaj(int a, int b) { DodajDo(a, b); return a; }
int Mnoz(int a, int b) { return (ll) a * b % mod; }
void MnozDo(int& a, int b) { a = Mnoz(a, b); }
int Pot(int a, ll b) { int res = 1; while (b) { if (b % 2 == 1) MnozDo(res, a); a = Mnoz(a, a); b /= 2; } return res; }
int Odw(int a) { return Pot(a, mod - 2); }
void PodzielDo(int& a, int b) { MnozDo(a, Odw(b)); }
int Podziel(int a, int b) { return Mnoz(a, Odw(b)); }
int Moduluj(ll x) { x %= mod; if (x < 0) x += mod; return x; }

template <typename T> T Maxi(T& a, T b) { return a = max(a, b); }
template <typename T> T Mini(T& a, T b) { return a = min(a, b); }

constexpr int nax = 25 * 1000 + 105;

int n, k;
vector<pair<pair<int, int>, int>> krawedzie;
vector<pair<int, int>> graf[nax];
short* lca[nax];
int waga[nax];
int parent[nax];

vector<int> preorder;
pair<int, int> subtree_range[nax];

void Dfs(int w, int o) {
  subtree_range[w].first = (int) preorder.size();
  preorder.push_back(w);
  for (const auto& [x, p] : graf[w]) {
    if (x == o) continue;
    parent[x] = w;
    waga[x] = p;
    Dfs(x, w);
  }
  subtree_range[w].second = (int) preorder.size() - 1;
}

inline uint64_t Combine(
    const uint64_t upper,
    const uint64_t lower) noexcept {
  // Murmur-inspired hashing.
  const uint64_t kMul = 0x9ddfea08eb382d69ULL;
  uint64_t a = (lower ^ upper) * kMul;
  a ^= (a >> 47);
  uint64_t b = (upper ^ a) * kMul;
  b ^= (b >> 47);
  b *= kMul;
  return b;
}

using Int = uint64_t;

template <int Dimen>
struct MojHash {};

template <>
struct MojHash<1> {
  size_t operator()(const tuple<Int>& val) const {
    return hash_(get<0>(val));
  };

  std::hash<Int> hash_;
};

template <>
struct MojHash<2> {
  size_t operator()(const tuple<Int, Int>& val) const {
    return hash_(Combine(get<0>(val), get<1>(val)));
  };

  std::hash<Int> hash_;
};

template <>
struct MojHash<4> {
  size_t operator()(const tuple<Int, Int, Int, Int>& val) const {
    return hash_(Combine(Combine(get<0>(val), get<1>(val)),
                         Combine(get<2>(val), get<3>(val))));
  };

  std::hash<Int> hash_;
};

template <>
struct MojHash<8> {
  size_t operator()(const tuple<Int, Int, Int, Int, Int, Int, Int, Int>& val) const {
    return hash_(Combine(Combine(Combine(get<0>(val), get<1>(val)),
                                 Combine(get<2>(val), get<3>(val))),
                         Combine(Combine(get<4>(val), get<5>(val)),
                                 Combine(get<6>(val), get<7>(val)))));
  };

  std::hash<Int> hash_;
};

template <>
struct MojHash<16> {
  size_t operator()(const tuple<Int, Int, Int, Int, Int, Int, Int, Int,
                                Int, Int, Int, Int, Int, Int, Int, Int>& val) const {
    return hash_(Combine(Combine(Combine(Combine(get<0>(val), get<1>(val)),
                                         Combine(get<2>(val), get<3>(val))),
                                 Combine(Combine(get<4>(val), get<5>(val)),
                                         Combine(get<6>(val), get<7>(val)))),
                         Combine(Combine(Combine(get<8>(val), get<9>(val)),
                                         Combine(get<10>(val), get<11>(val))),
                                 Combine(Combine(get<12>(val), get<13>(val)),
                                         Combine(get<14>(val), get<15>(val))))));
  };

  std::hash<Int> hash_;
};

template <int Dimen>
struct MojTupel {};

template <>
struct MojTupel<1> {
  using Tupel = tuple<Int>;
};

template <>
struct MojTupel<2> {
  using Tupel = tuple<Int, Int>;
};

template <>
struct MojTupel<4> {
  using Tupel = tuple<Int, Int, Int, Int>;
};

template <>
struct MojTupel<8> {
  using Tupel = tuple<Int, Int, Int, Int, Int, Int, Int, Int>;
};

template <>
struct MojTupel<16> {
  using Tupel = tuple<Int, Int, Int, Int, Int, Int, Int, Int,
                      Int, Int, Int, Int, Int, Int, Int, Int>;
};

int ile_jeszcze_moge[nax];
int ile_jeszcze_par;
bitset<nax> jeszcze_moge[nax];
int pula[nax];
Int wsp[nax];
int mabyc[nax];

vector<pair<int, int>> przedzialy;

template <int Dimen>
void StworzMape(
    unordered_map<typename MojTupel<Dimen>::Tupel, int,
                  MojHash<Dimen>>& counting,
    Int wartosc[nax][Dimen]) {
  counting.reserve(1 << 20);
  for (int w = 2; w <= n; w++) {
    for (int u = jeszcze_moge[w]._Find_first(); u < w;
         u = jeszcze_moge[w]._Find_next(u)) {
      typename MojTupel<Dimen>::Tupel tup;
      get<0>(tup) = wartosc[w][0] + wartosc[u][0] - 2 * wartosc[lca[w][u]][0];
      if constexpr (Dimen > 1) {
      get<1>(tup) = wartosc[w][1] + wartosc[u][1] - 2 * wartosc[lca[w][u]][1];
      if constexpr (Dimen > 2) {
      get<2>(tup) = wartosc[w][2] + wartosc[u][2] - 2 * wartosc[lca[w][u]][2];
      get<3>(tup) = wartosc[w][3] + wartosc[u][3] - 2 * wartosc[lca[w][u]][3];
      if constexpr (Dimen > 4) {
      get<4>(tup) = wartosc[w][4] + wartosc[u][4] - 2 * wartosc[lca[w][u]][4];
      get<5>(tup) = wartosc[w][5] + wartosc[u][5] - 2 * wartosc[lca[w][u]][5];
      get<6>(tup) = wartosc[w][6] + wartosc[u][6] - 2 * wartosc[lca[w][u]][6];
      get<7>(tup) = wartosc[w][7] + wartosc[u][7] - 2 * wartosc[lca[w][u]][7];
      if constexpr (Dimen > 8) {
      get<8>(tup) = wartosc[w][8] + wartosc[u][8] - 2 * wartosc[lca[w][u]][8];
      get<9>(tup) = wartosc[w][9] + wartosc[u][9] - 2 * wartosc[lca[w][u]][9];
      get<10>(tup) = wartosc[w][10] + wartosc[u][10] - 2 * wartosc[lca[w][u]][10];
      get<11>(tup) = wartosc[w][11] + wartosc[u][11] - 2 * wartosc[lca[w][u]][11];
      get<12>(tup) = wartosc[w][12] + wartosc[u][12] - 2 * wartosc[lca[w][u]][12];
      get<13>(tup) = wartosc[w][13] + wartosc[u][13] - 2 * wartosc[lca[w][u]][13];
      get<14>(tup) = wartosc[w][14] + wartosc[u][14] - 2 * wartosc[lca[w][u]][14];
      get<15>(tup) = wartosc[w][15] + wartosc[u][15] - 2 * wartosc[lca[w][u]][15];
      }
      }
      }
      }
      counting[tup]++;
    }
  }
}

template <int Dimen>
void CzyscJeszczeMoge(const typename MojTupel<Dimen>::Tupel maska,
                      Int wartosc[nax][Dimen]) {
  for (int w = 2; w <= n; w++) {
    ile_jeszcze_par -= ile_jeszcze_moge[w];
    for (int u = jeszcze_moge[w]._Find_first(); u < w;
         u = jeszcze_moge[w]._Find_next(u)) {
      typename MojTupel<Dimen>::Tupel tup;
      get<0>(tup) = wartosc[w][0] + wartosc[u][0] - 2 * wartosc[lca[w][u]][0];
      if constexpr (Dimen > 1) {
      get<1>(tup) = wartosc[w][1] + wartosc[u][1] - 2 * wartosc[lca[w][u]][1];
      if constexpr (Dimen > 2) {
      get<2>(tup) = wartosc[w][2] + wartosc[u][2] - 2 * wartosc[lca[w][u]][2];
      get<3>(tup) = wartosc[w][3] + wartosc[u][3] - 2 * wartosc[lca[w][u]][3];
      if constexpr (Dimen > 4) {
      get<4>(tup) = wartosc[w][4] + wartosc[u][4] - 2 * wartosc[lca[w][u]][4];
      get<5>(tup) = wartosc[w][5] + wartosc[u][5] - 2 * wartosc[lca[w][u]][5];
      get<6>(tup) = wartosc[w][6] + wartosc[u][6] - 2 * wartosc[lca[w][u]][6];
      get<7>(tup) = wartosc[w][7] + wartosc[u][7] - 2 * wartosc[lca[w][u]][7];
      if constexpr (Dimen > 8) {
      get<8>(tup) = wartosc[w][8] + wartosc[u][8] - 2 * wartosc[lca[w][u]][8];
      get<9>(tup) = wartosc[w][9] + wartosc[u][9] - 2 * wartosc[lca[w][u]][9];
      get<10>(tup) = wartosc[w][10] + wartosc[u][10] - 2 * wartosc[lca[w][u]][10];
      get<11>(tup) = wartosc[w][11] + wartosc[u][11] - 2 * wartosc[lca[w][u]][11];
      get<12>(tup) = wartosc[w][12] + wartosc[u][12] - 2 * wartosc[lca[w][u]][12];
      get<13>(tup) = wartosc[w][13] + wartosc[u][13] - 2 * wartosc[lca[w][u]][13];
      get<14>(tup) = wartosc[w][14] + wartosc[u][14] - 2 * wartosc[lca[w][u]][14];
      get<15>(tup) = wartosc[w][15] + wartosc[u][15] - 2 * wartosc[lca[w][u]][15];
      }
      }
      }
      }
      if (tup != maska) {
        jeszcze_moge[w][u] = false;
        ile_jeszcze_moge[w]--;
      }
    }
    ile_jeszcze_par += ile_jeszcze_moge[w];
  }
}

template <int Dimen>
void Faza(int prz_pos) {
  using Tupel = typename MojTupel<Dimen>::Tupel;

  static Int wspdim[nax][Dimen];
  static Int wartosc[nax][Dimen];

  // assert(prz_pos + Dimen <= (int) przedzialy.size());
  for (int dim = 0; dim < Dimen; dim++) {
    if (prz_pos + dim >= (int) przedzialy.size()) break;
    const auto [B, A] = przedzialy[prz_pos + dim];
    // cerr << "A: " << A << " B: " << B << endl;
    for (int i = A; i <= B; i++) {
      wspdim[i][dim] = wsp[i];
      debug() << imie(i) imie(dim) imie(wspdim[i][dim]);
    }
  }

  for (int w = 2; w <= n; w++) {
    wartosc[w][0] = wartosc[parent[w]][0] + wspdim[waga[w]][0];
    if constexpr (Dimen == 1) continue;
    wartosc[w][1] = wartosc[parent[w]][1] + wspdim[waga[w]][1];
    if constexpr (Dimen == 2) continue;
    wartosc[w][2] = wartosc[parent[w]][2] + wspdim[waga[w]][2];
    wartosc[w][3] = wartosc[parent[w]][3] + wspdim[waga[w]][3];
    if constexpr (Dimen == 4) continue;
    wartosc[w][4] = wartosc[parent[w]][4] + wspdim[waga[w]][4];
    wartosc[w][5] = wartosc[parent[w]][5] + wspdim[waga[w]][5];
    wartosc[w][6] = wartosc[parent[w]][6] + wspdim[waga[w]][6];
    wartosc[w][7] = wartosc[parent[w]][7] + wspdim[waga[w]][7];
    if constexpr (Dimen == 8) continue;
    wartosc[w][8] = wartosc[parent[w]][8] + wspdim[waga[w]][8];
    wartosc[w][9] = wartosc[parent[w]][9] + wspdim[waga[w]][9];
    wartosc[w][10] = wartosc[parent[w]][10] + wspdim[waga[w]][10];
    wartosc[w][11] = wartosc[parent[w]][11] + wspdim[waga[w]][11];
    wartosc[w][12] = wartosc[parent[w]][12] + wspdim[waga[w]][12];
    wartosc[w][13] = wartosc[parent[w]][13] + wspdim[waga[w]][13];
    wartosc[w][14] = wartosc[parent[w]][14] + wspdim[waga[w]][14];
    wartosc[w][15] = wartosc[parent[w]][15] + wspdim[waga[w]][15];
  }

  unordered_map<Tupel, int, MojHash<Dimen>> counting;
  StworzMape(counting, wartosc);

  vector<pair<Tupel, int>> posortowane;
  posortowane.reserve(counting.size());
  for (const auto& [maska, licznik] : counting) {
    posortowane.emplace_back(maska, licznik);
  }
  sort(posortowane.begin(), posortowane.end());

  Tupel maska;
  for (const auto& [maska_, licznik] : posortowane) {
    if (k <= licznik) {
      maska = maska_;
      break;
    } else {
      k -= licznik;
    }
  }
  // assert(maska != -1llu);

  CzyscJeszczeMoge(maska, wartosc);

  Int maska_rozloz[Dimen];
  maska_rozloz[0] = get<0>(maska);
  if constexpr (Dimen > 1) {
    maska_rozloz[1] = get<1>(maska);
    if constexpr (Dimen > 2) {
      maska_rozloz[2] = get<2>(maska);
      maska_rozloz[3] = get<3>(maska);
      if constexpr (Dimen > 4) {
        maska_rozloz[4] = get<4>(maska);
        maska_rozloz[5] = get<5>(maska);
        maska_rozloz[6] = get<6>(maska);
        maska_rozloz[7] = get<7>(maska);
        if constexpr (Dimen > 8) {
          maska_rozloz[8] = get<8>(maska);
          maska_rozloz[9] = get<9>(maska);
          maska_rozloz[10] = get<10>(maska);
          maska_rozloz[11] = get<11>(maska);
          maska_rozloz[12] = get<12>(maska);
          maska_rozloz[13] = get<13>(maska);
          maska_rozloz[14] = get<14>(maska);
          maska_rozloz[15] = get<15>(maska);
        }
      }
    }
  }

  for (int dim = 0; dim < Dimen; dim++) {
    if (prz_pos + dim >= (int) przedzialy.size()) break;
    const auto [B, A] = przedzialy[prz_pos + dim];
    for (int i = A; i <= B; i++) {
      wspdim[i][dim] = 0;
      mabyc[i] = maska_rozloz[dim] % (pula[i] + 1);
      maska_rozloz[dim] /= (pula[i] + 1);
      debug() << "mabyc[" << i << "] = " << mabyc[i];
    }
  }

  cerr << "Faza<" << Dimen << ">:";
  for (int dim = 0; dim < Dimen; dim++) {
    if (prz_pos + dim >= (int) przedzialy.size()) break;
    const auto [B, A] = przedzialy[prz_pos + dim];
    cerr << " [" << A << ", " << B << "]";
  }
  cerr << " | ile_jeszcze_par = " << ile_jeszcze_par
       << " | posortowane.size() = " << posortowane.size() << "\n";
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  cin >> n >> k;
  for (int i = 1; i < n; i++) {
    int a, b, p;
    cin >> a >> b >> p;
    assert(1 <= a and a <= n);
    assert(1 <= b and b <= n);
    assert(a != b);
    assert(1 <= p and p <= n);
    graf[a].emplace_back(b, p);
    graf[b].emplace_back(a, p);
    pula[p]++;
    krawedzie.emplace_back(make_pair(a, b), p);
  }

  // for (int i = 1; i <= n; i++) {
  //   cout << "pula[" << i << "] = " << pula[i] << endl;
  // }
  // return 0;

  for (int i = 1; i <= n; i++) {
    lca[i] = new short[i + 1];
  }

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j < i; j++) {
      lca[i][j] = 10;
    }
  }

  Dfs(1, -1);
  assert((int) preorder.size() == n);
  vector<int> nowy_numer(n + 1);
  for (int i = 0; i < n; i++) {
    nowy_numer[preorder[i]] = i + 1;
  }
  preorder.clear();
  for (int i = 1; i <= n; i++) {
    graf[i].clear();
  }
  for (const auto& [ab, p] : krawedzie) {
    auto [a, b] = ab;
    a = nowy_numer[a];
    b = nowy_numer[b];
    graf[a].emplace_back(b, p);
    graf[b].emplace_back(a, p);
  }
  Dfs(1, -1);
  assert((int) preorder.size() == n);
  for (int i = 0; i < n; i++) {
    assert(preorder[i] == i + 1);
  }

  cerr << "BEGIN LCA" << endl;

  for (int w = 1; w <= n; w++) {
    const int start = subtree_range[w].first;
    for (const auto& [x, p] : graf[w]) {
      (void) p;
      const int start_x = subtree_range[x].first;
      const int end_x = subtree_range[x].second;
      for (int i = start; i < start_x; i++) {
        for (int j = start_x; j <= end_x; j++) {
          lca[preorder[j]][preorder[i]] = w;
          // debug() << imie(j) imie(i) imie(w);
        }
      }
    }
  }

  cerr << "END LCA" << endl;

  for (int i = 1; i <= n; i++) {
    mabyc[i] = -1;
    jeszcze_moge[i].set();
    jeszcze_moge[i][0] = 0;
    ile_jeszcze_moge[i] = i - 1;
  }
  ile_jeszcze_par = n * (n - 1) / 2;

  int pozycja = n;
  while (pozycja >= 1) {
    const int first = pozycja;
    Int iloczyn = pula[pozycja] + 1;
    constexpr Int najwiecej = numeric_limits<Int>::max() / nax;
    if (pula[pozycja] <= 100) {
      while (pozycja > 1 and iloczyn * (pula[pozycja - 1] + 1) <= najwiecej) {
        if (pula[pozycja - 1] > 100) break;
        iloczyn *= (pula[pozycja - 1] + 1);
        pozycja--;
      }
    }

    Int ilo = 1;
    for (int i = pozycja; i <= first; i++) {
      wsp[i] = ilo;
      ilo *= (pula[i] + 1);
    }
    assert(ilo == iloczyn);

    debug() << imie(first) imie(pozycja) imie(ilo);

    przedzialy.emplace_back(first, pozycja);

    pozycja--;
  }
  assert(pozycja == 0);

  debug() << imie(przedzialy);

  int prz_pos = 0;
  while (prz_pos < (int) przedzialy.size()) {
    const int zostalo = (int) przedzialy.size() - prz_pos;
    assert(zostalo >= 1);
    if (zostalo <= 1 or
        przedzialy[prz_pos].first == przedzialy[prz_pos].second or
        przedzialy[prz_pos + 1].first == przedzialy[prz_pos + 1].second) {
      Faza<1>(prz_pos);
      prz_pos += 1;
    } else if (zostalo <= 2) {
      Faza<2>(prz_pos);
      prz_pos += 2;
    } else if (zostalo <= 4) {
      Faza<4>(prz_pos);
      prz_pos += 4;
    } else {
      Faza<8>(prz_pos);
      prz_pos += 8;
    }
  }

  int sumka = 0;
  for (int i = n; i >= 1; i--) {
    sumka = Dodaj(Mnoz(sumka, n), mabyc[i]);
  }
  MnozDo(sumka, n);
  cout << sumka << endl;
  return 0;
}