#include <iostream>
#include <vector>
#include <set>
using namespace std;
using pi = pair<int, int>;
using vi = vector<int>;
using vii = vector<pi>;
using vvi = vector<vi>;
using sii = set<pi>;
using si = set<int>;
int main() {
int n; cin >> n;
vvi marbles(n);
for(int i = 0; i < n; ++i) {
int k; cin >> k;
marbles[i].resize(k);
for(int j = 0; j < k; ++j) {
cin >> marbles[i][j];
}
}
set<pair<int, pair<int, int>>> d;
long long answer = 0;
long long modulo = 1e9 + 7;
for(int i = 0; i < n; ++i) {
d.insert({marbles[i][0], {i, 0}});
}
while(not d.empty()) {
long long digiti = d.begin()->first;
long long stacki = d.begin()->second.first;
long long indexi = d.begin()->second.second;
answer = (answer * 365 + digiti) % modulo;
d.erase(d.begin());
if((int)marbles[stacki].size() > indexi + 1) {
d.insert({marbles[stacki][indexi + 1], {stacki, indexi + 1}});
}
}
cout << (365 * answer) % modulo << endl;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8c2V0Pgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKdXNpbmcgcGkgPSBwYWlyPGludCwgaW50PjsKdXNpbmcgdmkgPSB2ZWN0b3I8aW50PjsKdXNpbmcgdmlpID0gdmVjdG9yPHBpPjsKdXNpbmcgdnZpID0gdmVjdG9yPHZpPjsKdXNpbmcgc2lpID0gc2V0PHBpPjsKdXNpbmcgc2kgPSBzZXQ8aW50PjsKCgppbnQgbWFpbigpIHsKICAgIGludCBuOyBjaW4gPj4gbjsKICAgIHZ2aSBtYXJibGVzKG4pOwogICAgZm9yKGludCBpID0gMDsgaSA8IG47ICsraSkgewogICAgICAgIGludCBrOyBjaW4gPj4gazsKICAgICAgICBtYXJibGVzW2ldLnJlc2l6ZShrKTsKICAgICAgICBmb3IoaW50IGogPSAwOyBqIDwgazsgKytqKSB7CiAgICAgICAgICAgIGNpbiA+PiBtYXJibGVzW2ldW2pdOwogICAgICAgIH0KICAgIH0KICAgIHNldDxwYWlyPGludCwgcGFpcjxpbnQsIGludD4+PiBkOwogICAgbG9uZyBsb25nIGFuc3dlciA9IDA7CiAgICBsb25nIGxvbmcgbW9kdWxvID0gMWU5ICsgNzsKICAgIGZvcihpbnQgaSA9IDA7IGkgPCBuOyArK2kpIHsKICAgICAgICBkLmluc2VydCh7bWFyYmxlc1tpXVswXSwge2ksIDB9fSk7CiAgICB9CiAgICB3aGlsZShub3QgZC5lbXB0eSgpKSB7CiAgICAgICAgbG9uZyBsb25nIGRpZ2l0aSA9IGQuYmVnaW4oKS0+Zmlyc3Q7CiAgICAgICAgbG9uZyBsb25nIHN0YWNraSA9IGQuYmVnaW4oKS0+c2Vjb25kLmZpcnN0OwogICAgICAgIGxvbmcgbG9uZyBpbmRleGkgPSBkLmJlZ2luKCktPnNlY29uZC5zZWNvbmQ7CiAgICAgICAgYW5zd2VyID0gKGFuc3dlciAqIDM2NSArIGRpZ2l0aSkgJSBtb2R1bG87CiAgICAgICAgZC5lcmFzZShkLmJlZ2luKCkpOwogICAgICAgIGlmKChpbnQpbWFyYmxlc1tzdGFja2ldLnNpemUoKSA+IGluZGV4aSArIDEpIHsKICAgICAgICAgICAgZC5pbnNlcnQoe21hcmJsZXNbc3RhY2tpXVtpbmRleGkgKyAxXSwge3N0YWNraSwgaW5kZXhpICsgMX19KTsgCiAgICAgICAgfSAKICAgIH0KICAgIGNvdXQgPDwgKDM2NSAqIGFuc3dlcikgJSBtb2R1bG8gIDw8IGVuZGw7CiAgICByZXR1cm4gMDsKfQo=