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
#include <bits/stdc++.h>
#include <unistd.h>
using namespace std;
#define REP(i,n) for(int _n=(n), i=0;i<_n;++i)
#define FOR(i,a,b) for(int i=(a),_b=(b);i<=_b;++i)
#define FORD(i,a,b) for(int i=(a),_b=(b);i>=_b;--i)
#define TRACE(x) cerr << "TRACE(" #x ")" << endl;
#define DEBUG(x) cerr << #x << " = " << (x) << endl;
typedef long long LL; typedef unsigned long long ULL;

class Input {
 public:
  Input() { bufpos = bufend = buffer; eof = false; }
  bool Eof() { return eof; }
  char Peek() { if(bufpos == bufend) Grab(); return *bufpos; }
  unsigned char UPeek() { return static_cast<unsigned char>(Peek()); }
  void SkipWS();
  template<class T> T Get();
  void operator()() {}
  template<class Arg, class... Args> void operator()(Arg &arg, Args &... args) {
    arg = Get<Arg>();
    operator()(args...);
  }
 private:
  static const int BUFSIZE = 1<<16;
  char buffer[BUFSIZE];
  char *bufpos;
  char *bufend;
  bool eof;
  void Grab();
};

void Input::Grab() {
  if(eof) return;
  bufpos = buffer;
  bufend = buffer + read(0, buffer, BUFSIZE);
  if(bufend==bufpos) { eof=true; *bufpos=0; }
}

template<> inline char Input::Get<char>() {
  char res = Peek();
  ++bufpos;
  return res;
}

void Input::SkipWS() {
  while(isspace(UPeek())) Get<char>();
}

template<> unsigned Input::Get<unsigned>() {
  SkipWS();
  unsigned x = 0;
  while(isdigit(UPeek())) {
    x = 10u * x + (Get<char>()-'0');
  }
  return x;
}

template<> int Input::Get<int>() {
  SkipWS();
  bool neg = false;
  if(Peek()=='-') { neg=true; Get<char>(); }
  unsigned x = Get<unsigned>();
  if (neg) x = -x;
  return static_cast<int>(x);
}

template<> ULL Input::Get<ULL>() {
  SkipWS();
  ULL x = 0;
  while(isdigit(UPeek())) {
    x = 10ULL * x + (Get<char>()-'0');
  }
  return x;
}

template<> LL Input::Get<LL>() {
  SkipWS();
  bool neg = false;
  if(Peek()=='-') { neg=true; Get<char>(); }
  ULL x = Get<ULL>();
  if (neg) x = -x;
  return static_cast<LL>(x);
}

template<> string Input::Get<string>() {
  SkipWS();
  string s;
  while(!Eof() && !isspace(UPeek())) s += Get<char>();
  return s;
}

Input IN;

template<unsigned MOD>
class Modulo {
 public:
  static constexpr unsigned modulus = MOD;

  Modulo(unsigned x=0):v(x) {}
  unsigned get() const { return v; }
  Modulo operator+(Modulo b) const {
    unsigned res = v+b.v;
    if (res >= MOD) res -= MOD;
    return res;
  }
  void operator+=(Modulo b) { *this = *this + b; }
  Modulo operator-(Modulo b) const { return *this + Modulo(MOD-b.v); }
  void operator-=(Modulo b) { *this = *this - b; }
  Modulo operator*(Modulo b) const { return Modulo(ULL(v) * ULL(b.v) % MOD); }
  void operator*=(Modulo b) { *this = *this * b; }

  bool operator==(Modulo b) const { return v == b.v; }
  bool operator!=(Modulo b) const { return v != b.v; }
 private:
  unsigned v;
};

using ModResult = Modulo<1'000'000'007>;
using ModHash = Modulo<(1u<<31)-1u>; // Mersenne prime

mt19937 rng(1005044525);

constexpr int hash_size = 2; // 62-bit hashing

class Hash {
 public:
  using Repr = array<ModHash, hash_size>;

  Hash() : val{} {}
  Hash(const Repr &_val) : val(_val) {}

  bool operator==(const Hash &other) const {
    return
      val[0] == other.val[0] &&
      val[1] == other.val[1];
  }

  bool operator!=(const Hash &other) const {
    return !operator==(other);
  }

  Hash operator+(const Hash &other) const {
    return Hash(Repr{
        val[0] + other.val[0],
        val[1] + other.val[1]});
  }

  void operator+=(const Hash &other) {
    *this = *this + other;
  }

  Hash operator-(const Hash &other) const {
    return Hash(Repr{
        val[0] - other.val[0],
        val[1] - other.val[1]});
  }

  void operator-=(const Hash &other) {
    *this = *this - other;
  }

  unsigned small_hash() const { return val[0].get(); }

 private:
  Repr val;
};

Hash random_hash() {
  uniform_int_distribution<unsigned> distr(0u, ModHash::modulus - 1u);
  Hash::Repr val;
  REP(i, hash_size) val[i] = distr(rng);
  return Hash(val);
}

constexpr int MAX_NUM_VERTICES = 25000;
constexpr int MAX_NUM_WEIGHTS = MAX_NUM_VERTICES;
constexpr int NUM_BUCKETS = 1<<14;

template <typename T>
class HashTable {
 public:
  HashTable() { clear(); }
  void clear();
  T *find(const Hash &hash);
  T *find_or_insert(const Hash &hash);

 private:
  struct Entry {
    Entry *next;
    Hash hash;
    T value;
  };
  Entry *buckets[NUM_BUCKETS];
  Entry entries[MAX_NUM_VERTICES];
  int num_entries;
};

template <typename T>
void HashTable<T>::clear() {
  REP(i,NUM_BUCKETS) buckets[i] = nullptr;
  num_entries = 0;
}

template <typename T>
T *HashTable<T>::find(const Hash &hash) {
  Entry *p = buckets[hash.small_hash() & (NUM_BUCKETS-1)];
  while (p) {
    if (p->hash == hash) return &p->value;
    p = p->next;
  }
  return nullptr;
}

template <typename T>
T *HashTable<T>::find_or_insert(const Hash &hash) {
  Entry *&first = buckets[hash.small_hash() & (NUM_BUCKETS-1)];
  Entry *p = first;
  while (p) {
    if (p->hash == hash) return &p->value;
    p = p->next;
  }
  p = &entries[num_entries++];
  p->hash = hash;
  p->value = T{};
  p->next = first;
  first = p;
  return &p->value;
}

struct Edge {
  int dest;
  int weight; // 0..num_vertices-1
};

struct EdgeForWeight {
  int a,b;
};

int num_vertices;
int num_weights;
int wanted_index;
vector<Edge> edges[MAX_NUM_VERTICES];
vector<EdgeForWeight> edges_for_weight[MAX_NUM_WEIGHTS];

void read_input() {
  IN(num_vertices, wanted_index);
  num_weights = num_vertices;
  --wanted_index;
  REP(i, num_vertices-1) {
    int a,b,w;
    IN(a,b,w);
    --a; --b; --w;
    edges[a].push_back(Edge{b,w});
    edges[b].push_back(Edge{a,w});
    edges_for_weight[w].push_back(EdgeForWeight{a,b});
  }
}

Hash weight_hashes[MAX_NUM_WEIGHTS];

struct BfsEntry {
  int vertex;
  int parent;
  int num_special;
  int last_special_vertex;
  Hash big_weight_hash;
};

// to avoid reallocating
namespace solve_one_weight_vars {
  int connection_count[MAX_NUM_VERTICES];
  BfsEntry *last_bfs_entry[2];
  BfsEntry bfs_entries[2][MAX_NUM_VERTICES];
  HashTable<int> hash_tables[2];
}

// Return the count for the given weight and modify index.
int solve_one_weight(int special_weight, const Hash &desired_big_weight_hash, int &index) {
  using namespace solve_one_weight_vars;

  const int num_special_edges = edges_for_weight[special_weight].size();
  FOR(count,1,num_special_edges) connection_count[count] = 0;

  for (EdgeForWeight starting_edge : edges_for_weight[special_weight]) {
    REP(direction, 2) {
      hash_tables[direction].clear();

      {
        BfsEntry &start_entry = bfs_entries[direction][0];
        start_entry.vertex = starting_edge.b;
        start_entry.parent = starting_edge.a;
        start_entry.num_special = 0;
        start_entry.last_special_vertex = starting_edge.b;
        start_entry.big_weight_hash = Hash{};
      }
      BfsEntry *next_entry = &bfs_entries[direction][1];
      for (BfsEntry *entry = &bfs_entries[direction][0];
           entry != next_entry;
           ++entry) {
        if (entry->num_special == 0) {
          int *cnt = hash_tables[direction].find_or_insert(desired_big_weight_hash - entry->big_weight_hash);
          ++*cnt;
        }

        for (const Edge &e : edges[entry->vertex]) {
          if (e.dest == entry->parent) continue;
          next_entry->vertex = e.dest;
          next_entry->parent = entry->vertex;
          next_entry->num_special = entry->num_special;
          next_entry->last_special_vertex = entry->last_special_vertex;
          next_entry->big_weight_hash = entry->big_weight_hash;
          if (e.weight == special_weight) {
            ++next_entry->num_special;
            next_entry->last_special_vertex = e.dest;
          } else if (e.weight > special_weight) {
            next_entry->big_weight_hash += weight_hashes[e.weight];
          }
          ++next_entry;
        }
      }
      last_bfs_entry[direction] = next_entry;
      swap(starting_edge.a, starting_edge.b);
    }

    REP(direction, 2) {
      for (BfsEntry *entry = bfs_entries[direction];
           entry != last_bfs_entry[direction];
           ++entry) {
        if (entry->last_special_vertex > starting_edge.a) {
          int *cnt = hash_tables[direction^1].find(entry->big_weight_hash);
          if (cnt) {
            connection_count[entry->num_special+1] += *cnt;
          }
        }
      }
      swap(starting_edge.a, starting_edge.b);
    }
  }

  // connection_count[0] is not computed, but not required

  int special_count = num_special_edges;
  while (special_count > 0) {
    int cc = connection_count[special_count];
    if (index < cc) break;
    index -= cc;
    --special_count;
  }
  return special_count;
}

int weight_counts[MAX_NUM_WEIGHTS];

void solve() {
  int total_connections = num_vertices * (num_vertices-1) / 2;
  // Easier to count from largest rather than smallest.
  int index = total_connections - 1 - wanted_index;
  Hash desired_big_weight_hash;

  FORD(special_weight, num_weights-1, 0) {
    int special_count = solve_one_weight(special_weight, desired_big_weight_hash, index);
    weight_counts[special_weight] = special_count;
    weight_hashes[special_weight] = random_hash();
    REP(i, special_count) desired_big_weight_hash += weight_hashes[special_weight];
  }
}

ModResult calc_total_tax() {
  ModResult exponential_tax = 1;
  ModResult total_tax = 0;
  REP(weight, num_weights) {
    exponential_tax *= num_vertices;
    total_tax += exponential_tax * weight_counts[weight];
  }

  return total_tax;
}

int main() {
  read_input();
  solve();
  ModResult total_tax = calc_total_tax();
  cout << total_tax.get() << "\n";
}