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
#include <bits/stdc++.h>
using namespace std;
long long n, ti[102], t[102][102], k, k2, dp[102][30], pot[102][30];
long long find_dp(int i)
{
    long long fdp = 0;
    for (int it = 0; it < 30; ++it)
    {
        fdp += dp[i][it] * pot[ti[i]][it];
    }
    return fdp;
}
void multi(int i)
{
    for (int it = 0; it < 30; ++it)
    {
        dp[i][it] *= k2;
    }
    for (int it = 0; it < 30; ++it)
    {
        dp[i][it + 1] += dp[i][it] / 1000000;
        dp[i][it] %= 1000000;
    }
}
void push_dp(int i, int j)
{
    long long a = 0;
    for (int it = 29; it >= 0; --it)
    {
        dp[j][it] += (dp[i][it] + a) / ti[i];
        dp[j][it + 1] += dp[j][it] / 1000000;
        dp[j][it] %= 1000000;
        long long a2 = (a + dp[i][it]) % ti[i];
        a = a2 * 1000000;
    }
}
int main()
{
    for (int i = 2; i <= 100; ++i)
    {
        pot[i][1] = 1000000 % i;
        pot[i][0] = 1;
    }
    for (int i = 2; i <= 100; ++i)
    {
        for (int it = 2; it < 30; ++it)
        {
            pot[i][it] = pot[i][it - 1] * pot[i][1];
            pot[i][it] %= i;
        }
    }
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i)
    {
        scanf("%d", &ti[i]);
        for (int j = 0; j < ti[i]; ++j)
        {
            scanf("%d", &t[i][j]);
        }
    }
    if (ti[1] == 0)
    {
        printf("1");
        return 0;
    }
    dp[0][0] = ti[1];
    dp[1][0] = ti[1];
    for (int i = 1; i <= n; ++i)
    {
        if (ti[i] == 0)
            continue;
        long long mod_dp = find_dp(i) % ti[i];
        //cout << dp[i][0] << ' ' << dp[i][1] << ' ' << dp[i][2] << endl;
        //cout << mod_dp << ' ' << ti[i] << endl;
        if (mod_dp != 0)
        {
            k2 = ti[i] / __gcd(mod_dp, ti[i]);
            for (int j = i; j <= n; ++j)
            {
                multi(j);
            }
            multi(0);
        }
        for (int j = 0; j < ti[i]; ++j)
        {
            push_dp(i, t[i][j]);
        }
    }
    int it = 29;
    while (dp[0][it] == 0)
        --it;
    printf("%d", dp[0][it]);
    while (it > 0)
    {
        --it;
        int it2 = -1;
        while (dp[0][it] >= pow(10, it2 + 1))
            ++it2;
        it2 = 5 - it2;
        for (int i = 0; i < it2; ++i)
            printf("0");
        if (dp[0][it] != 0)
            printf("%d", dp[0][it]);
    }
    return 0;
}