Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(300000)

MOD = 1000000007  # type: int

def solve(N: int, K: int, a: "List[int]", b: "List[int]"):
    conn = [[] for _ in range(N)]
    for i in range(N - 1):
        conn[a[i] - 1].append(b[i] - 1)
        conn[b[i] - 1].append(a[i] - 1)

    def dfs(idx, cur, tmp, visited, ret):
        visited[idx] = True
        #print(idx, cur, tmp)

        #tmp[idx] = max(K - cur, 0)
        ret *= max(K - cur, 0)
        ret %= MOD

        cur = min(cur + 1, 2)
        for nex in conn[idx]:
            if not visited[nex]:
                ret = dfs(nex, cur, tmp, visited, ret)
                cur += 1
        return ret

    tmp = [0] * N
    visited = [False] * N
    ret = 1
    ret = dfs(0, 0, tmp, visited, ret)
    #print(tmp)

    #for t in tmp:
    #    ret *= t
    #    ret %= MOD

    print(ret)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    a = [int()] * (N-1)  # type: "List[int]" 
    b = [int()] * (N-1)  # type: "List[int]" 
    for i in range(N-1):
        a[i] = int(next(tokens))
        b[i] = int(next(tokens))
    solve(N, K, a, b)

if __name__ == '__main__':
    main()


Fixed submission:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(300000)

MOD = 1000000007  # type: int

def solve(N: int, K: int, a: "List[int]", b: "List[int]"):
    conn = [[] for _ in range(N)]
    for i in range(N - 1):
        conn[a[i] - 1].append(b[i] - 1)
        conn[b[i] - 1].append(a[i] - 1)

    def dfs(idx, cur, tmp, visited, ret):
        visited[idx] = True
        #print(idx, cur, tmp)

        #tmp[idx] = max(K - cur, 0)
        ret *= max(K - cur, 0)
        ret %= MOD

        cur = min(cur + 1, 2)
        for nex in conn[idx]:
            if not visited[nex]:
                ret = dfs(nex, cur, tmp, visited, ret)
                cur += 1
        return ret

    tmp = [0] * N
    visited = [False] * N
    ret = 1
    ret = dfs(0, 0, tmp, visited, ret)
    #print(tmp)

    #for t in tmp:
    #    ret *= t
    #    ret %= MOD

    print(ret)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    a = [int()] * (N-1)  # type: "List[int]" 
    b = [int()] * (N-1)  # type: "List[int]" 
    for i in range(N-1):
        a[i] = int(next(tokens))
        b[i] = int(next(tokens))
    solve(N, K, a, b)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(300000)

MOD = 1000000007  # type: int

def solve(N: int, K: int, a: "List[int]", b: "List[int]"):
    conn = [[] for _ in range(N)]
    for i in range(N - 1):
        conn[a[i] - 1].append(b[i] - 1)
        conn[b[i] - 1].append(a[i] - 1)

    def dfs(idx, cur, tmp, visited):
        visited[idx] = True
        #print(idx, cur, tmp)
        tmp[idx] = max(K - cur, 0)
        cur = min(cur + 1, 2)
        for nex in conn[idx]:
            if not visited[nex]:
                tmp = dfs(nex, cur, tmp, visited)
                cur += 1
        return tmp

    tmp = [0] * N
    visited = [False] * N
    tmp = dfs(0, 0, tmp, visited)
    #print(tmp)

    ret = 1
    for t in tmp:
        ret *= t
        ret %= MOD

    print(ret)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    a = [int()] * (N-1)  # type: "List[int]" 
    b = [int()] * (N-1)  # type: "List[int]" 
    for i in range(N-1):
        a[i] = int(next(tokens))
        b[i] = int(next(tokens))
    solve(N, K, a, b)

if __name__ == '__main__':
    main()


Fixed submission:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(300000)

MOD = 1000000007  # type: int

def solve(N: int, K: int, a: "List[int]", b: "List[int]"):
    conn = [[] for _ in range(N)]
    for i in range(N - 1):
        conn[a[i] - 1].append(b[i] - 1)
        conn[b[i] - 1].append(a[i] - 1)

    def dfs(idx, cur, tmp, visited, ret):
        visited[idx] = True
        #print(idx, cur, tmp)

        #tmp[idx] = max(K - cur, 0)
        ret *= max(K - cur, 0)
        ret %= MOD

        cur = min(cur + 1, 2)
        for nex in conn[idx]:
            if not visited[nex]:
                ret = dfs(nex, cur, tmp, visited, ret)
                cur += 1
        return ret

    tmp = [0] * N
    visited = [False] * N
    ret = 1
    ret = dfs(0, 0, tmp, visited, ret)
    #print(tmp)

    #for t in tmp:
    #    ret *= t
    #    ret %= MOD

    print(ret)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    a = [int()] * (N-1)  # type: "List[int]" 
    b = [int()] * (N-1)  # type: "List[int]" 
    for i in range(N-1):
        a[i] = int(next(tokens))
        b[i] = int(next(tokens))
    solve(N, K, a, b)

if __name__ == '__main__':
    main()