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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#include <iostream>
#include <vector>
#include <algorithm>
#include <random>
#include <ctime>

using namespace std;


int to_digit(char c) {
    return c - '0';
}

char to_char(int x) {
    return '0' + x;
}

void trim(string& s) {
    for (int i=s.size()-1; i>=0; i--) {
        if (s[i] == '0') {
            s.pop_back();
        } else {
            break;
        }
    }
}

void add(string& s1, string& s2) {
    int mem = 0;
    int i = 0;
    for (; i<s1.size(); i++) {
        int res = to_digit(s1[i]) + mem;
        if (i<s2.size()) {
            res += to_digit(s2[i]);
        }
        
        s1[i] = to_char(res % 10);
        mem = res / 10;
    }
    
    for (; i<s2.size(); i++) {
        int res = to_digit(s2[i]) + mem;
        s1.push_back(to_char(res % 10));
        mem = res / 10;
    }
    
    if (mem != 0) {
        s1.push_back(to_char(mem));
    }
    trim(s1);
}

bool greater_or_equal(string& s1, string& s2) {
    if (s1.size() > s2.size()) {
        return true;
    }
    if (s1.size() < s2.size()) {
        return false;
    }
    
    for (int i=s1.size()-1; i>=0; i--) {
        if (s1[i] > s2[i]) {
            return true;
        }
        if (s1[i] < s2[i]) {
            return false;
        }
    }
    
    return true;
}

void sub(string& s1, string& s2) {
    int borr = 0;
    for (int i=0; i<s1.size(); i++) {
        int res = to_digit(s1[i]) + borr;
        borr = 0;
        
        if (i<s2.size()) {
            res -= to_digit(s2[i]);
        }
        
        if (res < 0) {
            borr = -1;
            res += 10;
        }
        s1[i] = to_char(res);
    }
    
    trim(s1);
}

pair<string, string> div_rem(string& s1, string& s2) {
    string div_res = "";
    string rem_res = s1;
    
    if (s1.size() < s2.size()) {
        return make_pair(div_res, rem_res);
    }
    int ten_pow=s1.size()-s2.size();
    
    string zeros = string(ten_pow, '0');
    for (int i=ten_pow; i>=0; i--) {
    //cout << "sizes " << zeros.size() << " " << s2.size() << endl;
        string substractor = zeros + s2;
        
        int count = 0;
        while (greater_or_equal(rem_res, substractor)) {
            count++;
            sub(rem_res, substractor);
        }
        div_res.push_back(to_char(count));
        zeros.pop_back();
    }
    
    reverse(div_res.begin(), div_res.end());
    trim(div_res);
    trim(rem_res);
    return make_pair(div_res, rem_res);
}

string mul_int(string& s, int v) {
    string mul_res;
    int mem = 0;
    
    for (int i=0; i<s.size(); i++) {
        int res = to_digit(s[i])*v + mem;
        
        mul_res.push_back(to_char(res % 10));
        mem = res/10;
    }
    
    while (mem != 0) {
        mul_res.push_back(to_char(mem % 10));
        mem /= 10;
    }
    
    trim(mul_res);
    return mul_res;
}

string mul(string& s1, string& s2) {
    string mul_res;
    int mem = 0;
    string zeros;
    
    for (int i=0; i<s2.size(); i++) {
        string multiplied_s1 = zeros + s1;
        string partial = mul_int(multiplied_s1, to_digit(s2[i]));
        add(mul_res, partial);
        zeros.push_back('0');
    }
    
    trim(mul_res);
    return mul_res;
}

bool randomBool() {
    return (rand() % 100) < 10;
}







void multiply_vec(vector<unsigned long long> &vec, unsigned long long num) {
    for (int i=0; i<vec.size(); i++) {
        vec[i] = vec[i] * num;
    }
}

void smultiply_vec(vector<string> &vec, string &num) {
    for (int i=0; i<vec.size(); i++) {
        vec[i] = mul(vec[i], num);
    }
}

unsigned long long gcd(unsigned long long a, unsigned long long b) {
    while (b != 0) {
        long b_val = b;
        b = a % b;
        a = b_val;        
    }
    
    return a;
}

string sgcd(string a, string b) {
    string one("1");
    while (greater_or_equal(b, one)) {
        string b_val = b;
        b = div_rem(a, b).second;
        a = b_val;        
    }
    
    return a;
}

vector<vector<int>> construct_max(int n) {
    vector<vector<int>> graph(1);
    srand(2137);
    
    for (int i=1; i<=n; i++) {
        vector<int> neighbors;
        
        
        if (i==77) {
            graph.push_back(neighbors);
            continue;
        }
        for (int j=i+1; j<=n; j++) {
            if (i==84) {
                continue;
            }
            if (randomBool()) {
                neighbors.push_back(j);
            }
        }
        graph.push_back(neighbors);
    }
    return graph;
}

int main() {
    int n;
    cin >> n;
    vector<vector<int>> graph(1);
    
    for (int i=0; i<n; i++) {
        int c;
        cin >> c;
        vector<int> neighbors;
        //cout << c << endl;
        for (int j=0; j<c; j++) {
            int v;
            cin >> v;
            neighbors.push_back(v);
        }
        
        //cout << i  << ": " << neighbors.size() << endl;
        graph.push_back(neighbors);
    }
    
    //cout << "15 "  << ": " << graph[15].size() << endl;
    
    // n=100;
    // graph = construct_max(100);
    
    // cout << 10;
    
    // for (auto vec: graph) {
    //     cout << vec.size() << " ";
    //     for (int i: vec) {
    //         cout << i << " ";
    //     }
    //     cout<<endl;
    // }
    
    vector<string> edges(n+1, "");
    for (int v: graph[1]) {
        edges[v] = "1";
    }
    // cout << "before computing "<< endl;
    //     for (int i=0; i<edges.size(); i++) {
    //         if (edges[i].size() == 0) {
    //             cout << 0 << " ";
    //         } else {
    //             cout << edges[i] << " ";
    //         }
    //     }
    // cout << endl;
    
    for (int next=2; next<=n; next++) {
        if (edges[next].size() == 0) { // no connection from start
            continue;
        }
        
        
        if (graph[next].size() == 0) { // leaf
            continue;
        }
        
        string outgoing_edges = to_string(graph[next].size());
        reverse(outgoing_edges.begin(), outgoing_edges.end());
        string incoming_edges = edges[next];
        
        
        string sgdc_res = sgcd(incoming_edges, outgoing_edges);
        string multiplier = div_rem(outgoing_edges, sgdc_res).first;
        smultiply_vec(edges, multiplier);


        string split = div_rem(edges[next], outgoing_edges).first;
        
        // cout << "inc " << incoming_edges << endl;
        // cout << "out " << outgoing_edges << endl;
        // cout << "gcd " << sgdc_res << endl;
        // cout << "mult " << multiplier << endl;
        // cout << "split " << split << endl;
        
        
        edges[next] = "";
        for (int outgoing : graph[next]) {
            add(edges[outgoing], split);
        }
        
        // cout << "after computing " << next << endl;
        // for (int i=0; i<edges.size(); i++) {
        //     if (edges[i].size() == 0) {
        //         cout << 0 << " ";
        //     } else {
        //         cout << edges[i] << " ";
        //     }
        // }
        // cout << endl;
    }
    
    string result = "";
    for (int i=1; i<edges.size(); i++) {
        add(result, edges[i]);
    }

    trim(result);
    if (result.size() == 0) {
        result = "1";
    }
    
    reverse(result.begin(), result.end());
    cout << result << endl;
    
    
    // string a("271368712638172");
    // reverse(a.begin(), a.end());
    // string b("777777777777777777777777");
    // reverse(b.begin(), b.end());
    
    // string g = sgcd(a, b);
    // reverse(g.begin(), g.end());
    // string m = mul(a, b);
    // reverse(m.begin(), m.end());
    // pair<string, string> dr = div_rem(a, b);
    // string d = dr.first;
    // reverse(d.begin(), d.end());
    // string r = dr.second;
    // reverse(r.begin(), r.end());
    // cout << "res " << d << " " << r << endl;
    // cout << "res " << g << endl;
    // cout << "res " << m;
    // cout << div_rem(a, b).first << " " << div_rem(a, b).second << endl;
    // cout << 213873612873 / 1273612763 << " " << 213873612873 % 1273612763 << endl;
    return 0;
}