1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import math

def lcm(a, b):
    gcdAb = math.gcd(a, b)
    return (a // gcdAb) * b

def add(fraction, addend):
    targetDenom = lcm(fraction[1], addend[1])

    fraction = (fraction[0] * (targetDenom // fraction[1]), targetDenom)

    normAddend = (addend[0] * (targetDenom // addend[1]), targetDenom)

    fraction = (fraction[0] + normAddend[0], fraction[1])

    fractionGcd = math.gcd(fraction[0], fraction[1])
    return (fraction[0] // fractionGcd, fraction[1] // fractionGcd)

platformsNum = int(input())
outEdges = {}
inEdges = {}
caseFractions = []

for i in range(0, platformsNum):
    inEdges[i] = []
    outEdges[i] = []
    caseFractions.append((0, 1))

for i in range(0, platformsNum):
    for platform in list(map(int, input().split(" ")))[1:]:
        outEdges[i].append(platform - 1);
        inEdges[platform - 1].append(i);

cases = len(outEdges[0])
caseFractions[0] = (1, 1)

for i in range(1, platformsNum):
    inPlatforms = inEdges[i]

    if (len(inPlatforms) == 0 or len(outEdges[i]) == 0):
        continue

    for j in range(0, len(inPlatforms)):
        platform = inPlatforms[j]
        
        inFraction = caseFractions[platform]
        addend = (inFraction[0], inFraction[1] * len(outEdges[platform]))

        caseFractions[i] = add(caseFractions[i], addend)
        

    fraction = caseFractions[i]
    if (fraction[0] > 0):
        currCases = (fraction[0] * cases) // fraction[1]
        casesMult = lcm(currCases, len(outEdges[i])) // currCases

        cases *= casesMult

if cases == 0:
    cases = 1

print(str(cases))