N,K = gets.chomp.split(" ").map(&:to_i)
mat = readlines.map{|n| n.chomp.split(" ").map(&:to_i)}


@graph = Array.new(N+1){[]}
mat.each do |n|
    arr = @graph[n[0]]
    arr << n[1]
    @graph[n[0]] = arr
    arr = @graph[n[1]]
    arr << n[0]
    @graph[n[1]] = arr
end
def log(n)
    #p n
end

@q = []
@visited = []
@color_nums = [] #nodes color number
def dfs(v, minus)
    cands_number = nil
    if minus == -1
        cands_number = K - 1
    else
        cands_number = K - 2
    end
    @visited[v] = true
    num=1

    arr = @graph[v]
    arr.each do |n|
        next if @visited[n]
        num*= cands_number
        cands_number-=1
        num %= 1000000007
    end
    arr.each do |n|
        if not @visited[n]
            num*=dfs(n,minus-1)
            num %= 1000000007
        end
    end
    return num;
end
puts K * dfs(1,-1) % 1000000007