Language: C++

import java.io.PrintWriter
import java.util.*
import java.math.*
import java.lang.*
import kotlin.comparisons.*

val pw = PrintWriter(System.out)
val MOD: Long = (1e+9 + 7).toLong()

class Dfs(private val graph: List<List<Int>>) {
    fun dfs(k: Int, now: Int, from: Int): Long {
        var canUseColorNum = if(from == -1) k-1 else k-2 

        if(k < graph[now].size) { //塗れる色が足りない場合
            return 0L
        } else { // 色が足りている時
            var caseNum = 1L
            graph[now].forEach { e ->
                if(e != from) {
                    caseNum *= canUseColorNum
                    caseNum %= MOD
                    canUseColorNum--
                }
            }

            graph[now].forEach { e ->
                if(e != from) {
                    caseNum *= dfs(k, e, now)
                    caseNum %= MOD
                }
            }
            return caseNum
        }
    }
}

fun main(args: Array<String>) {
    val (n, k) = readListOfInt()

    val graph = (0 until n).map { mutableListOf<Int>() }.toMutableList()
    for(i in 0 until n-1) {
        val (a, b) = readListOfInt().map { it-1 }
        graph[a].add(b)
        graph[b].add(a)
    }
    val count = (k * Dfs(graph).dfs(k, 0, -1)) % MOD
    println(count)


    pw.flush()
}

/****** Declared Functions and Data Structures ******/
// IO
fun read() = readLine()!!
fun readInt() = read().toInt()
fun readLong() = read().toLong()
fun readDouble() = read().toDouble()
fun readListOfString() = read().split(" ")
fun readListOfInt() = readListOfString().map { it.toInt() }
fun readListOfLong() = readListOfString().map { it.toLong() }
fun readListOfDouble() = readListOfString().map { it.toDouble() }

fun Double.format(digits: Int) = String.format("%.${digits}f", this)
fun Float.format(digits: Int) = String.format("%.${digits}f", this)

fun print(value: Any) { pw.print(value) }
fun println(value : Any) { pw.println(value) }


// Extensions
fun<T> List<T>.toBuckets(bucketKeys: List<T>): Map<T, Int> {
    val buckets = bucketKeys.associate { t -> Pair(t, 0) }.toMutableMap()
    this.forEach { t ->
        buckets[t] = buckets[t]!! + 1
    }
    return buckets
}

fun <K, V> Map<K, V>.toMutableMap(): MutableMap<K, V> = HashMap(this)

// Util Functions
fun<T> permutations(src: List<T>): List<List<T>> {
    if(src.size == 1) return listOf(src)
    val perms = mutableListOf<List<T>>()
    val insertElement = src[0]
    permutations(src.drop(1)).forEach { perm ->
        for(i in 0..perm.size) {
            val newPerm = perm.toMutableList()
            newPerm.add(i, insertElement)
            perms.add(newPerm.toList())
        }
    }
    return perms
}

// return nCr, if you want nCr, please access v[n][r]
fun combinations(n: Long): List<List<Long>> {
    val v = (0..n).map { (0..n).map{0L}.toMutableList() }.toMutableList()
    for(i in 0 until v.size) {
        v[i][0] = 1L
        v[i][i] = 1L
    }
    for(i in 0 until v.size) {
        for(j in 1 until i) {
            v[i][j] = (v[i-1][j-1] + v[i-1][j]) % MOD
        }
    }
    return v.map { it.toList() }.toList()
}

// should be a >= b
fun gcd(a: Long, b: Long): Long = 
    if(b == 0L) a else gcd(b, a % b)

// shoud be a >= b
fun lcm(a: Long, b: Long): Long = 
    a / gcd(a, b) * b

fun sumDigits(num_: Long): Long {
    var num = num_
    var rtn: Long = 0
    while(num != 0.toLong()) {
        val tmp = num % 10
        rtn += tmp
        num /= 10
    }
    return rtn
}

fun Double.isDecimal(): Boolean = ((this - Math.floor(this)) != 0.0)

// Data Structures
class Stack<T>(private var st: MutableList<T> = mutableListOf<T>()) {
    fun isEmpty() = st.isEmpty()
    fun top(): T = st.last()
    fun push(e: T) { st.add(e) }
    fun pop() { st = st.dropLast(1).toMutableList() }
}

class Queue<T>(private var que: MutableList<T> = mutableListOf<T>()) {
    fun isEmpty() = que.isEmpty()
    fun top(): T = que.first()
    fun push(e: T) { que.add(e) }
    fun pop() { que = que.drop(1).toMutableList() }
}


Compiler output:
a.cc:88:18: error: too many decimal points in number
   88 |         for(i in 0..perm.size) {
      |                  ^~~~~~~~~~~~
a.cc:99:14: error: too many decimal points in number
   99 |     val v = (0..n).map { (0..n).map{0L}.toMutableList() }.toMutableList()
      |              ^~~~
a.cc:99:27: error: too many decimal points in number
   99 |     val v = (0..n).map { (0..n).map{0L}.toMutableList() }.toMutableList()
      |                           ^~~~
a.cc:1:1: error: 'import' does not name a type
    1 | import java.io.PrintWriter
      | ^~~~~~
a.cc:1:1: note: C++20 'import' only available with '-fmodules-ts'
a.cc:37:1: error: 'fun' does not name a type
   37 | fun main(args: Array<String>) {
      | ^~~
a.cc:55:1: error: 'fun' does not name a type
   55 | fun read() = readLine()!!
      | ^~~
a.cc:61:1: error: 'fun' does not name a type
   61 | fun readListOfLong() = readListOfString().map { it.toLong() }
      | ^~~
a.cc:62:1: error: 'fun' does not name a type
   62 | fun readListOfDouble() = readListOfString().map { it.toDouble() }
      | ^~~
a.cc:64:1: error: 'fun' does not name a type
   64 | fun Double.format(digits: Int) = String.format("%.${digits}f", this)
      | ^~~
a.cc:68:1: error: 'fun' does not name a type
   68 | fun println(value : Any) { pw.println(value) }
      | ^~~
a.cc:72:1: error: 'fun' does not name a type
   72 | fun<T> List<T>.toBuckets(bucketKeys: List<T>): Map<T, Int> {
      | ^~~
a.cc:80:1: error: 'fun' does not name a type
   80 | fun <K, V> Map<K, V>.toMutableMap(): MutableMap<K, V> = HashMap(this)
      | ^~~
a.cc:98:1: error: 'fun' does not name a type
   98 | fun combinations(n: Long): List<List<Long>> {
      | ^~~
a.cc:113:1: error: 'fun' does not name a type
  113 | fun gcd(a: Long, b: Long): Long =
      | ^~~
a.cc:131:1: error: 'fun' does not name a type
  131 | fun Double.isDecimal(): Boolean = ((this - Math.floor(this)) != 0.0)
      | ^~~
a.cc:141:7: error: 'Queue' is not a class template
  141 | class Queue<T>(private var que: MutableList<T> = mutableListOf<T>()) {
      |       ^~~~~
a.cc:141:13: error: 'T' was not declared in this scope
  141 | class Queue<T>(private var que: MutableList<T> = mutableListOf<T>()) {
      |             ^
a.cc:141:16: error: expected unqualified-id before 'private'
  141 | class Queue<T>(private var que: MutableList<T> = mutableListOf<T>()) {
      |                ^~~~~~~
a.cc:141:16: error: expected ')' before 'private'
  141 | class Queue<T>(private var que: MutableList<T> = mutableListOf<T>()) {
      |               ~^~~~~~~
      |                )