#pragma GCC optimize("Ofast")
#pragma GCC target("avx")
#include <bits/stdc++.h>
using namespace std;

#define INIT std::cin.tie(0);std::ios::sync_with_stdio(false);
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define REP(i, n) for (int i = 0; i < static_cast<int>(n); i++)
#define FOR(i, s, n) for (int i = s; i < static_cast<int>(n); i++)
#define ASC(t) [](t& x, t& y){ return x < y; }
#define DESC(t) [](t& x, t& y){ return x > y; }
#define PSORT(s,p,e) partial_sort((s), next((s), (p)), (e))
#define VIEW(v,t) for_each((v).begin(), (v).end(), [](t& a) { cout << a << " "; }); cout << endl;

#define IN(a,x,b) (a<=x && x<=b)
#define WS std::cout<<" ";
#define NL std::cout<<"\n"
#define OUT(dist) std::cout<<(dist);
template<class T> inline T CHMAX(T& x, const T y){return x=(x<y)?y:x;}
template<class T> inline T CHMIN(T& x, const T y){return x=(x>y)?y:x;}

using PII = pair<int, int>;
using PLL = pair<long long, long long>;
using LL = long long;
using ULL = unsigned long long;
template<class T> using V = vector<T>;
template<class T> using V2 = vector<vector<T>>;

template<class T=int, T P=1000000007>
struct ModuloP {
    T x;
    ModuloP() : x(0) {}
    ModuloP(T y) { x = y%P; if(x < 0) x += P; }
    ModuloP operator+(const ModuloP y) { return (ModuloP(x)+=y); }
    ModuloP operator*(const ModuloP y) { return (ModuloP(x)*=y); }
    ModuloP operator-(const ModuloP y) { return (ModuloP(x)-=y); }
    ModuloP operator/(const ModuloP y) { return (ModuloP(x)/=y); }
    ModuloP operator-() { return (x == 0) ? ModuloP(P-x) : ModuloP(x); }
    ModuloP& operator+=(const ModuloP y) { x+=y.x; if(x>=P)x-=P; return *this; }
    ModuloP& operator*=(const ModuloP y) { x=((LL)x*(LL)y.x)%P; return *this; }
    ModuloP& operator-=(const ModuloP y) { x+=P-y.x; if(x>=P)x-=P; return *this; }
    //ModuloP& operator/=(ModuloP y) { return ((*this)*=y.fermetinv()); } // fermet
    ModuloP& operator/=(ModuloP y) { return ((*this)=ModuloP(x)*mod_inv(y)); } // ext_gcd
    bool operator==(const ModuloP y) { return x == y.x; }
    bool operator!=(const ModuloP y) { return x != y.x; }
    bool operator <(const ModuloP y) { return x < y.x; }
    bool operator <=(const ModuloP y) { return x <= y.x; }
    bool operator >(const ModuloP y) { return x > y.x; }
    bool operator >=(const ModuloP y) { return x >= y.x; }

    ModuloP pow(LL n) { // O(log n)
        ModuloP ans(1), t(x);
        while(n) {
            if(n & 1) ans *= t;
            t *= t; n >>= 1;
        }
        return ans;
    }

    ModuloP fermetinv() { return pow(P-2); }

    static T ext_gcd(T a, T b, T& l, T& m){
        T res = a;
        if(b != 0) res = ext_gcd(b, a%b, m, l), m-=(a / b)*l;
        else l = 1, m = 0;
        return res;
    }   

    static ModuloP mod_inv(ModuloP z){
        T l, m; 
        ext_gcd(z.x, P, l, m);
        return ModuloP(P+ModuloP(l).x);
    }

    static ModuloP combination(T n, int r){
        ModuloP m(1),fr(1); 
        REP(i,r) m*=ModuloP(n-i), fr*=ModuloP(i+1);
        return m/fr;
    }

    static ModuloP permutation(T n, int r){
        if(n < r) return ModuloP(0);
        ModuloP m(1);  
        REP(i,r) m*=ModuloP(n-i);
        return m;
    }
};
using MP = ModuloP<>;

int N, K;
V2<int> g;

MP dfs(int v, int par, int gpar) {
    int r = g[v].size();
    MP res = res.permutation(K-2,r-1);
    if(par == -1 && gpar == -1) res = res.permutation(K-1,r);
    for(auto to : g[v]) {
        if(to == par) continue;
        res *= dfs(to, v, par);
    }
    return res;
}

int main(){
    INIT;
    cin >> N >> K;
    g.resize(N,V<int>());
    REP(i,N-1) {
        int a, b; cin >> a >> b;
        a--; b--;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    int root = 0;
    cout << (MP(K)*dfs(root, -1, -1)).x;
}

Measured memory_bytes: 10368