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
// Artur Kraska, II UWr

#include <bits/stdc++.h>

#define forr(i, n)                  for(int i=0; i<n; i++)
#define FOREACH(iter, coll)         for(auto iter = coll.begin(); iter != coll.end(); ++iter)
#define FOREACHR(iter, coll)        for(auto iter = coll.rbegin(); iter != coll.rend(); ++iter)
#define lbound(P,R,PRED)            ({typeof(P) X=P,RRR=(R), PPP = P; while(PPP<RRR) {X = (PPP+(RRR-PPP)/2); if(PRED) RRR = X; else PPP = X+1;} PPP;})
#define testy()                     int _tests; scanf("%d", &_tests); FOR(_test, 1, _tests)
#define CLEAR(tab)                  memset(tab, 0, sizeof(tab))
#define CONTAIN(el, coll)           (coll.find(el) != coll.end())
#define FOR(i, a, b)                for(int i=a; i<=b; i++)
#define FORD(i, a, b)               for(int i=a; i>=b; i--)
#define MP                          make_pair
#define PB                          push_back
#define deb(X)                      X;

#define M 1000000007
#define INF 1000000007LL

using namespace std;

int n, k, a, b, w, teraz;
long long pot[100007];
vector <long long> wagi;
vector <vector<int>> wektory;

struct kraw
{
    int dl;
    int cel;
};
kraw kr;

struct wierzch
{
    vector <kraw> l;
    int byl;
};
wierzch tab[100007];

void dfs_wagi(int nr, int korzen, int dl, long long waga)
{
//    cout << "wlazi do " << nr << endl;
    tab[nr].byl = teraz;
    waga += pot[dl-1];
    if(nr > korzen)
    {
        wagi.PB(waga);
    }
    for(auto nb : tab[nr].l)
    {
        if(tab[nb.cel].byl != teraz)
        {
            dfs_wagi(nb.cel, korzen, nb.dl, waga);
        }
    }
}

void dfs_wektory(int nr, int korzen, int dl, vector <int> v)
{
//    cout << "wlazi do " << nr << endl;
    tab[nr].byl = teraz;
    vector <int> v2(v);
    v2.PB(-dl);
    if(nr > korzen)
    {
        wektory.PB(v2);
    }
    for(auto nb : tab[nr].l)
    {
        if(tab[nb.cel].byl != teraz)
        {
            dfs_wektory(nb.cel, korzen, nb.dl, v2);
        }
    }
}

int main()
{
    scanf("%d %d", &n, &k);
    int maxw = 0;
    forr(i, n-1)
    {
        scanf("%d %d %d", &a, &b, &w);
        maxw = max(w, maxw);
        kr.cel = b;
        kr.dl = w;
        tab[a].l.PB(kr);
        kr.cel = a;
        tab[b].l.PB(kr);
    }

    if(maxw <= 4)
    {
        pot[0] = 1;
        FOR(i, 1, n+1)
            pot[i] = pot[i-1]*n;

        FOR(i, 1, n-1)
        {
            teraz++;
            dfs_wagi(i, i, 1, -1);
        }

        sort(wagi.begin(), wagi.end());

//        for(auto w : wagi)
//            cout << "waga: " << w*n << endl;

        printf("%lld\n", wagi[k-1]%M*n%M);
    }
    else
    {
        pot[0] = 1;
        FOR(i, 1, n+1)
            pot[i] = (pot[i-1]*n)%M;

        FOR(i, 1, n-1)
        {
            teraz++;
            vector <int> v;
            dfs_wektory(i, i, 0, v);
        }

        FOREACH(it, wektory)
        {
            sort(it->begin(), it->end());
        }
        sort(wektory.begin(), wektory.end());
/*
        for(auto v : wektory)
        {
            for(auto el : v)
            {
                cout << " " << el;
            }
            cout << endl;
        }
*/
        long long res = M-1;
        for(auto el: wektory[n*(n-1)/2-k])
        {
            res += pot[-el];
        }

        printf("%lld\n", res%M);

    }

    return 0;
}