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
#include <bits/stdc++.h>

using namespace std;

const int N = 25005, M = 1e9 + 7;

int n, pot[N], a, b, c;
long long k;
vector<pair<int, int> > V[N];

// vector<map<int, int> > allPaths;
map<int, int> path;
int root;
struct rosn {
bool operator()(const map<int, int>& a, const map<int, int>& b) const {
    auto itA = a.begin();
    auto itB = b.begin();
    while (itA != a.end()) {
        if (itB == b.end()) {
            return false;
        }
        if (itA->first != itB->first) {
            return -itA->first < -itB->first;
        }
        if (itA->second != itB->second) {
            return itA->second < itB->second;
        }
        ++itA;
        ++itB;
    }
    return itB != b.end();
}
};

struct malej {
bool operator()(const map<int, int>& a, const map<int, int>& b) const {
    auto itA = a.begin();
    auto itB = b.begin();
    while (itA != a.end()) {
        if (itB == b.end()) {
            return true;
        }
        if (itA->first != itB->first) {
            return -itA->first > -itB->first;
        }
        if (itA->second != itB->second) {
            return itA->second > itB->second;
        }
        ++itA;
        ++itB;
    }
    return false;
}
};

multiset<map<int, int>, rosn> allPathsRos;
multiset<map<int, int>, malej> allPathsMal;
void DFSros(int w, int par) {
    if (!path.empty() && w > root) {
        allPathsRos.insert(path);
        if (allPathsRos.size() > k) {
            allPathsRos.erase(std::prev(allPathsRos.end()));
        }
    }
    for (auto p : V[w]) {
        int u = p.first;
        int wyk = p.second;
        if (u == par) continue;
        path[-wyk]++;
        DFSros(u, w);
        path[-wyk]--;
        if (path[-wyk] == 0) {
            path.erase(-wyk);
        }
    }
}

void DFSmal(int w, int par) {
    if (!path.empty() && w > root) {
        allPathsMal.insert(path);
        if (allPathsMal.size() > k) {
            allPathsMal.erase(std::prev(allPathsMal.end()));
        }
    }
    for (auto p : V[w]) {
        int u = p.first;
        int wyk = p.second;
        if (u == par) continue;
        path[-wyk]++;
        DFSmal(u, w);
        path[-wyk]--;
        if (path[-wyk] == 0) {
            path.erase(-wyk);
        }
    }
}

void add(int &w, int u) {
    w += u;
    if (w >= M) {
        w -= M;
    }
}

int mul(int w, int u) {
    return (long long)w * u % M;
}

void wypiszMape(map<int, int> m) {
    for (auto it : m) {
        cerr << it.first << " " << it.second << endl;
    }
}

int main() {
    
    scanf("%d %lld", &n, &k);
    bool rosnace = true;
    if ((long long)n * (n - 1) / 4 < k) {
        rosnace = false;
        k = (long long)n * (n - 1) / 2 - k + 1;
    }
    
    pot[0] = 1;
    for (int i = 1; i <= n; i++) {
        pot[i] = mul(pot[i - 1], n);
    }
    
    for (int i = 1; i < n; i++) {
        scanf("%d %d %d", &a, &b, &c);
        V[a].push_back({b, c});
        V[b].push_back({a, c});
    }

    
    for (int i = 1; i <= n; i++) {
        root = i;
        if (rosnace)
        DFSros(i, -1);
        else
        DFSmal(i, -1);
    }
    
    int ans = 0;

    if (rosnace) {
        auto winner = *(--allPathsRos.end());
        for (auto it : winner) {
            add(ans, mul(pot[-it.first], it.second));
        }
    } else {
        auto winner = *(--allPathsMal.end());
        for (auto it : winner) {
            add(ans, mul(pot[-it.first], it.second));
        }
    }
    
    printf("%d\n", ans);
    
    return 0;
}