module queue
  implicit none
  type :: JagArray
    integer,allocatable:: array(:)
  end type JagArray
  contains
  subroutine push(A,b)
    integer,allocatable,dimension(:)::A
    integer::b
    A=[A,b]
  end subroutine
  subroutine pop(A)
    integer,allocatable,dimension(:)::A
    A=A(2:)
  end subroutine
end module queue

use queue
integer N,K
integer A,B
type(JagArray),allocatable,dimension(:)::EDGE
integer(8) ans
integer,allocatable,dimension(:)::childs
integer,allocatable,dimension(:)::option
integer mo
read*,N,K
allocate(EDGE(N),option(N),childs(N))
mo=10**9+7
do i=1,N
  EDGE(i)%array=[integer::]
end do
do i=1,N-1
  read*,A,B
  call push(EDGE(A)%array,B)
  call push(EDGE(B)%array,A)
end do
do i=1,N
  childs(i)=size(EDGE(i)%array)
end do

option=-1
option(1)=K
call DFS(1)

ans=1
do i=1,N
  ans=mod(ans*option(i),mo)
end do

print"(i0)",ans

contains
recursive subroutine DFS(x)
  implicit none 
  integer::x
  integer::color
  integer::i
  color=K-2
  if(x==1)color=K-1
  do i=1,childs(x)
    if(option(EDGE(x)%array(i))/=-1)cycle
    option(EDGE(x)%array(i))=color
    color=color-1
    if(color==-1)exit
    call DFS(EDGE(x)%array(i))
  end do
end subroutine

end