Problem p02985 (python)

Fixed submission:
import sys
from collections import deque
sys.setrecursionlimit(1000000007)
input = sys.stdin.readline

MOD = 1000000007

class Node:
    def __init__(self):
        self.check = False
        self.link = []

    def color(self, K):
        self.check = True
        ret = 1
        # 子の塗り分け方を計算
        for i in range(len(self.link)-1):
            ret *= (K - 2) - i
            ret %= MOD
        # 子の子の塗り分け方を掛ける
        for node in self.link:
            if not node.check:
                ret *= node.color(K)
                ret %= MOD
        return ret

def main():
    N, K = map(int, input().split())
    if N == 1:
        print(K)
    else:
        nodes = [Node() for i in range(N+1)]
        for i in range(N-1):
            a, b = map(int, input().split())
            a -= 1; b -= 1;
            nodes[a].link.append(nodes[b])
            nodes[b].link.append(nodes[a])
        root = nodes[0]
        root.check = True
        ans = K
        for i in range(1, len(root.link)+1):
            ans *= (K-i)
            ans %= MOD
        for node in root.link:
            ans *= node.color(K)
            ans %= MOD
        print(ans)

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import sys
from collections import deque
input = sys.stdin.readline

MOD = 1000000007

class Node:
    def __init__(self):
        self.check = False
        self.link = []

    def color(self, K):
        self.check = True
        ret = 1
        # 子の塗り分け方を計算
        for i in range(len(self.link)-1):
            ret *= (K - 2) - i
            ret %= MOD
        # 子の子の塗り分け方を掛ける
        for node in self.link:
            if not node.check:
                ret *= node.color(K)
                ret %= MOD
        return ret

def main():
    N, K = map(int, input().split())
    if N == 1:
        print(K)
    else:
        nodes = [Node() for i in range(N+1)]
        for i in range(N-1):
            a, b = map(int, input().split())
            a -= 1; b -= 1;
            nodes[a].link.append(nodes[b])
            nodes[b].link.append(nodes[a])
        root = nodes[0]
        root.check = True
        ans = K
        for i in range(1, len(root.link)+1):
            ans *= (K-i)
            ans %= MOD
        for node in root.link:
            ans *= node.color(K)
            ans %= MOD
        print(ans)

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import sys
from collections import deque
input = sys.stdin.readline

MOD = 1000000007

class Node:
    def __init__(self):
        self.check = False
        self.link = []

    def color(self, K):
        self.check = True
        ret = 1
        # 子の塗り分け方を計算
        for i in range(len(self.link)-1):
            ret *= (K - 2) - i
            ret %= MOD
        # 子の子の塗り分け方を掛ける
        for node in self.link:
            if not node.check:
                ret *= node.color(K)
                ret %= MOD
        return ret

def main():
    N, K = map(int, input().split())
    nodes = [Node() for i in range(N+1)]
    for i in range(N-1):
        a, b = map(int, input().split())
        a -= 1; b -= 1;
        nodes[a].link.append(nodes[b])
        nodes[b].link.append(nodes[a])
    root = nodes[0]
    ans = K * (K - 1)
    ans *= root.color(K)
    ans %= MOD
    print(ans)

if __name__ == "__main__":
    main()