Language: C++

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;

#define FOR(i,x,y) for(ll i=(ll)x; i<(ll)y; ++i)
#define REP(i,y) FOR(i, 0, y)
#define pb push_back

constexpr ll MOD = (ll)((1E+9)+7);

vvll Path;
vll flag;
vll K1;
vll K2;

void K_init(ll K){
  K1.resize(K);
  K1[0] = K-1;
  FOR(i,1,K) K1[i]=(K1[i-1]*(K-1-i))%MOD;

  K2.resize(K-1);
  K2[0] = K-2;
  FOR(i,1,K-1) K2[i]=(K2[i-1]*(K-2-i))%MOD;
}

ll calc_K1(ll x){
  if(x<0) return 0;
  if(x>=K1.size()-1) return 0;
  return K1[x];
}

ll calc_K2(ll x){
  if(x<0) return 0;
  if(x>=K2.size()-1) return 0;
  return K2[x];
}

ll calc2(ll pos){
  if(!flag[pos]) return 1;
  flag[pos] = false;
  if(Path[pos].size()==0) return 0;
  if(Path[pos].size()==1) return 1;
  ll tmp = 1;
  REP(i,Path[pos].size()){
    tmp *= calc2(Path[pos][i]);
    tmp %= MOD;
  }
  //fprintf(stderr, "%lld %lld\n", pos, tmp);
  tmp *= calc_K2(Path[pos].size()-2);
  tmp %= MOD;
  //fprintf(stderr, "%lld %lld\n", pos, tmp);
  return tmp;
}

ll calc(ll pos){
  if(!flag[pos]) return 1;
  flag[pos] = false;
  if(Path[pos].size()==0) return 0;
  ll tmp = 1;
  REP(i,Path[pos].size()){
    tmp *= calc2(Path[pos][i]);
    tmp%=MOD;
  }
  tmp *= calc_K1(Path[pos].size()-1);
  tmp %= MOD;
  //fprintf(stderr, "%lld %lld\n", pos, tmp);
  return tmp%MOD;
}

int main(){
  cin.tie(0); // cut the cin and cout (default, std::flush is performed after std::cin)
  ios::sync_with_stdio(false); // cut the iostream and stdio (DON'T endl; BUT "\n";)

  ll N, K;
  cin >> N >> K;
  Path.resize(N);
  flag.resize(N);
  REP(i,N) flag[i] = true;
  REP(i,N-1){
    ll a, b;
    cin >> a >> b;
    a--; b--;
    Path[a].pb(b);
    Path[b].pb(a);
  }
  K_init(K);

  printf("%lld\n", (calc(0)*K)%MOD);

  return 0;
}
da-no-MacBook-Air:133 fockl$ vi E.cpp
da-no-MacBook-Air:133 fockl$ cat E.cpp
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;

#define FOR(i,x,y) for(ll i=(ll)x; i<(ll)y; ++i)
#define REP(i,y) FOR(i, 0, y)
#define pb push_back

constexpr ll MOD = (ll)((1E+9)+7);

vvll Path;
vll flag;
vll K1;
vll K2;

void K_init(ll K){
  K1.resize(K);
  K1[0] = K-1;
  FOR(i,1,K) K1[i]=(K1[i-1]*(K-1-i))%MOD;

  K2.resize(K-1);
  K2[0] = K-2;
  FOR(i,1,K-1) K2[i]=(K2[i-1]*(K-2-i))%MOD;
}

ll calc_K1(ll x){
  if(x<0) return 0;
  if(x>=K1.size()-1) return 0;
  return K1[x];
}

ll calc_K2(ll x){
  if(x<0) return 0;
  if(x>=K2.size()-1) return 0;
  return K2[x];
}

ll calc2(ll pos){
  if(!flag[pos]) return 1;
  flag[pos] = false;
  if(Path[pos].size()==0) return 0;
  if(Path[pos].size()==1) return 1;
  ll tmp = 1;
  REP(i,Path[pos].size()){
    tmp *= calc2(Path[pos][i]);
    tmp %= MOD;
  }
  //fprintf(stderr, "%lld %lld\n", pos, tmp);
  tmp *= calc_K2(Path[pos].size()-2);
  tmp %= MOD;
  //fprintf(stderr, "%lld %lld\n", pos, tmp);
  return tmp;
}

ll calc(ll pos){
  if(!flag[pos]) return 1;
  flag[pos] = false;
  if(Path[pos].size()==0) return 0;
  ll tmp = 1;
  REP(i,Path[pos].size()){
    tmp *= calc2(Path[pos][i]);
    tmp%=MOD;
  }
  tmp *= calc_K1(Path[pos].size()-1);
  tmp %= MOD;
  //fprintf(stderr, "%lld %lld\n", pos, tmp);
  return tmp%MOD;
}

int main(){
  cin.tie(0); // cut the cin and cout (default, std::flush is performed after std::cin)
  ios::sync_with_stdio(false); // cut the iostream and stdio (DON'T endl; BUT "\n";)

  ll N, K;
  cin >> N >> K;
  Path.resize(N);
  flag.resize(N);
  REP(i,N) flag[i] = true;
  REP(i,N-1){
    ll a, b;
    cin >> a >> b;
    a--; b--;
    Path[a].pb(b);
    Path[b].pb(a);
  }
  K_init(K);

  printf("%lld\n", (calc(0)*K)%MOD);

  return 0;
}

Compiler output:
a.cc:95:1: error: 'da' does not name a type
   95 | da-no-MacBook-Air:133 fockl$ vi E.cpp
      | ^~
a.cc:108:14: error: redefinition of 'constexpr const ll MOD'
  108 | constexpr ll MOD = (ll)((1E+9)+7);
      |              ^~~
a.cc:12:14: note: 'constexpr const ll MOD' previously defined here
   12 | constexpr ll MOD = (ll)((1E+9)+7);
      |              ^~~
a.cc:110:6: error: redefinition of 'vvll Path'
  110 | vvll Path;
      |      ^~~~
a.cc:14:6: note: 'vvll Path' previously declared here
   14 | vvll Path;
      |      ^~~~
a.cc:111:5: error: redefinition of 'vll flag'
  111 | vll flag;
      |     ^~~~
a.cc:15:5: note: 'vll flag' previously declared here
   15 | vll flag;
      |     ^~~~
a.cc:112:5: error: redefinition of 'vll K1'
  112 | vll K1;
      |     ^~
a.cc:16:5: note: 'vll K1' previously declared here
   16 | vll K1;
      |     ^~
a.cc:113:5: error: redefinition of 'vll K2'
  113 | vll K2;
      |     ^~
a.cc:17:5: note: 'vll K2' previously declared here
   17 | vll K2;
      |     ^~
a.cc:115:6: error: redefinition of 'void K_init(ll)'
  115 | void K_init(ll K){
      |      ^~~~~~
a.cc:19:6: note: 'void K_init(ll)' previously defined here
   19 | void K_init(ll K){
      |      ^~~~~~
a.cc:125:4: error: redefinition of 'll calc_K1(ll)'
  125 | ll calc_K1(ll x){
      |    ^~~~~~~
a.cc:29:4: note: 'll calc_K1(ll)' previously defined here
   29 | ll calc_K1(ll x){
      |    ^~~~~~~
a.cc:131:4: error: redefinition of 'll calc_K2(ll)'
  131 | ll calc_K2(ll x){
      |    ^~~~~~~
a.cc:35:4: note: 'll calc_K2(ll)' previously defined here
   35 | ll calc_K2(ll x){
      |    ^~~~~~~
a.cc:137:4: error: redefinition of 'll calc2(ll)'
  137 | ll calc2(ll pos){
      |    ^~~~~
a.cc:41:4: note: 'll calc2(ll)' previously defined here
   41 | ll calc2(ll pos){
      |    ^~~~~
a.cc:154:4: error: redefinition of 'll calc(ll)'
  154 | ll calc(ll pos){
      |    ^~~~
a.cc:58:4: note: 'll calc(ll)' previously defined here
   58 | ll calc(ll pos){
      |    ^~~~
a.cc:169:5: error: redefinition of 'int main()'
  169 | int main(){
      |     ^~~~
a.cc:73:5: note: 'int main()' previously defined here
   73 | int main(){
      |     ^~~~