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
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <cassert>

using namespace std;

// #define DEBUG
#ifdef DEBUG
#define dassert(x) assert(x);
#define df(...) printf(__VA_ARGS__)
#else
#define dassert(x)
#define df(...)
#endif

#define x first
#define y second
#define mp make_pair
#define pb push_back
#define ir(x, a, b) ((a) <= (x) && (x) <= (b))
#define vec vector
#define sz(x) (ll)x.size()
#define foru(i, n) for (int i = 0; (i) < (n); ++(i))
#define all(x) (x).begin(), (x).end()
#define fori(i, a, b) for (int i = (a); i < (b); ++i)

typedef int64_t ll;

int read() {
    int n = 0; bool b = 0; char c = getchar();
    for (; !ir(c, '0', '9'); c = getchar()) b = (c == '-');
    for (; ir(c, '0', '9'); c = getchar()) n = 10*n + (c-'0');
    if (b) return -n;
    return n;
}

const ll MOD = 1e9;
struct num {
    vec<int> t;

    num() {}
    num(int a) {
        if (a) t = {a};
    }

    num operator *(num other) const {
        const vec<int>& a = t;
        const vec<int>& b = other.t;
        vec<int> c(a.size()+b.size());

        foru (i, a.size()) {
            for (int j=0, carry=0; j<(int)b.size() || carry; ++j) {
                long long cur = c[i+j] + a[i] * 1ll * (j < (int)b.size() ? b[j] : 0) + carry;
                // df("adding %lld\n", cur);
                c[i+j] = int (cur % MOD);
                carry = int (cur / MOD);
            }
        }
        while (c.size() > 1 && c.back() == 0)
            c.pop_back();
        num res;
        res.t = c;
        return res;
    }

    num operator +(num other) const {
        vec<int> a = t;
        const vec<int>& b = other.t;

        int carry = 0;
        for (size_t i=0; i<max(a.size(),b.size()) || carry; ++i) {
            if (i == a.size())
                a.push_back (0);
            a[i] += carry + (i < b.size() ? b[i] : 0);
            carry = a[i] >= MOD;
            if (carry)  a[i] -= MOD;
        }
        num res;
        res.t = a;
        return res;
    }

    pair<int, num> operator /(int b) {
        if (b == 1) return {1, *this};
        vec<int> a = t;

        int carry = 0;
        for (int i=(int)a.size()-1; i>=0; --i) {
            long long cur = a[i] + carry * 1ll * MOD;
            a[i] = int (cur / b);
            carry = int (cur % b);
        }
        while (a.size() > 1 && a.back() == 0)
            a.pop_back();
        num res;
        res.t = a;
        return {carry, res};
    }

    void print() {
        printf ("%d", t.empty() ? 0 : t.back());
        for (int i=(int)t.size()-2; i>=0; --i)
            printf ("%09d", t[i]);
    }
};

int main() {
    df("debug mode\n");
#ifndef DEBUG
    // ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#endif
    // auto [a, b] = num(1)/2;
    // cout << a << " ";
    // b.print();
    // cout << endl;
    // return 0;
    int n;
    cin >> n;
    vec<vec<int>> g(n);
    foru (i, n) {
        int r;
        cin >> r;
        foru (_, r) {int x; cin >> x; g[i].pb(--x); }
    }
    
    vec<num> dp(n);
    // num res(1);
    
    dp[0] = num(1);
    num denom = 1;
    foru (i, n) {
        if (!g[i].size()) continue;
        
        // printf("dp is ");
        // dp[i].print();
        df("\n%d have to pass through\n", g[i].size());
        auto [rem, q] = dp[i]/g[i].size();
        // printf("rem: %d ", rem);
        // q.print(); printf("\n");
        if (rem > 0) {
            int gc = gcd(rem, g[i].size()), mul = g[i].size()/gc;
            if (gc > 1) {
                auto [rem2, q2] = dp[i]/gc;
                assert(rem2 == 0);
                dp[i] = q2;
            }
            
            df("multiplying denom (and the rest) by %d\n", mul);
            denom = denom * mul;
            for (int j = i+1; j < n; ++j) {
                dp[j] = dp[j] * num(mul);
            }
        } else {
            dp[i] = q;
        }
        // denom.print(); printf("\n");
        
        // dp[i].simplify();
        // df("dp[%d] = %d / %d\n", i, dp[i].a, dp[i].b);

        for (auto c : g[i]) {
            // df("adding %d/%d to %d/%d\n", dp[i].a, dp[i].b, dp[c].a, dp[c].b);
            dp[c] = (dp[c] + dp[i]);
        }
    }
    
    denom.print();

    return 0;
}