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
#include<bits/stdc++.h>
using namespace std;
 
ostream& operator<<(ostream &out, string str) {
	for(char c : str) out << c;
	return out;
}
 
template<class L, class R> ostream& operator<<(ostream &out, pair<L, R> p) {
	return out << "(" << p.first << ", " << p.second << ")";
}
 
template<class T> auto operator<<(ostream &out, T a) -> decltype(a.begin(), out) {
	out << "{";
	for(auto it = a.begin(); it != a.end(); it = next(it))
		out << (it != a.begin() ? ", " : "") << *it;
	return out << "}";
}
 
void dump() { cerr << "\n"; }
template<class T, class... Ts> void dump(T a, Ts... x) {
	cerr << a << ", ";
	dump(x...);
}
 
#ifdef DEBUG
#  define debug(...) cerr << "[" #__VA_ARGS__ "]: ", dump(__VA_ARGS__)
#else
#  define debug(...) false
#endif
 
#define REP(i, n) for(int i = 0; i < n; i++)
#define FOR(i, a, b) for(int i = a; i <= b; i++)
#define ST first
#define ND second
 
template<class T> int size(T && a) { return a.size(); }
 
using LL = long long;
using PII = pair<int, int>;

mt19937_64 rng(2137); //chrono::system_clock::now().time_since_epoch().count());
LL rd(LL a, LL b) {
	return uniform_int_distribution<LL>(a, b)(rng);
}

vector<vector<PII>> adj;
vector<bool> done;
vector<int> sub, par, path, ans, res;
vector<LL> w;

void dfs(int v) {
	sub[v] = 1;
	for(auto [u, p] : adj[v]) {
		if(!done[u] && u != par[v]) {
			par[u] = v; dfs(u);
			sub[v] += sub[u];
		}
	}
}
int centro(int v) {
	par[v] = -1; dfs(v);
	for(int sz = sub[v];;) {
		pair<int, int> mx = {0, 0};
		for(auto [u, p] : adj[v])
			if(!done[u] && u != par[v])
				mx = max(mx, {sub[u], u});
		if(mx.first * 2 <= sz) return v;
		v = mx.second;
	}
}

LL hsh, full;
int cur = 0;
map<LL, vector<int>> act;

void count(int v) {
	if(size(act[hsh]) <= path[cur]) 
		act[hsh].resize(path[cur] + 1);
	act[hsh][path[cur]]++;

	for(auto [u, p] : adj[v]) {
		if(!done[u] && u != par[v]) {
			path[p]++, hsh += w[p];
			par[u] = v; 
			count(u);
			path[p]--, hsh -= w[p];
		}
	}
}

void decomp(int v) {
	done[v = centro(v)] = true;

	map<LL, vector<int>> cnt;
	cnt[0].emplace_back(1);

	for(auto [u, p] : adj[v]) {
		if(!done[u]) {
			path[p]++, hsh += w[p];
			par[u] = v;
			act.clear();
			count(u); 
		
			for(auto& [h, v1] : act) {
				auto &v2 = cnt[full - h];
				REP(j, size(v2)) REP(i, size(v1)) 
					ans[i + j] += v1[i] * v2[j];
			}
			for(auto& [h, v] : act) {
				if(size(cnt[h]) < size(v))
					cnt[h].resize(size(v));
				REP(i, size(v))
					cnt[h][i] += v[i];
			}

			path[p]--, hsh -= w[p];
		}
	}

	for(auto [u, p] : adj[v]) 
		if(!done[u]) 
			decomp(u);
}

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

	int n, k;
	cin >> n >> k;

	adj.resize(n);
	vector<int> p_cnt(n);
	REP(i, n - 1) {
		int a, b, p;
		cin >> a >> b >> p;
		adj[a - 1].emplace_back(b - 1, p - 1);
		adj[b - 1].emplace_back(a - 1, p - 1);
		p_cnt[p - 1]++;
	}

	sub = par = path = res = vector<int>(n);
	done.resize(n);
	ans.resize(n + 1);
	w.resize(n);

	for(cur = n - 1; cur >= 0; cur--) {
		if(!p_cnt[cur]) continue;
		REP(i, n) done[i] = false;
		REP(i, n + 1) ans[i] = 0;

		decomp(0);

		w[cur] = rd(1, 1e12);
		FOR(i, 0, n) {
			if(ans[i] >= k) {
				res[cur] = i;
				full += w[cur] * i;
				break;
			}
			else k -= ans[i];
		}
	}

	LL x = n, s = 0, mod = 1e9 + 7;
	REP(i, n) {
		s = (s + x * res[i]) % mod;
		x = x * n % mod;
	}

	cout << s << "\n";
}