#include using namespace std; int main(){ ios_base::sync_with_stdio(false); long long x = 0, cache_n = 0; cin >> x; vector> line (x + 1,{0,0}); long long dangerous = 0; for(long long i = 0; i < x; i++){ long long birds; cin >> birds; vector next (birds, 0); for(long long j = 0; j < birds; j++){ long long index; cin >> index; if (index + 1 >= cache_n){ dangerous += line[index + 1].second; } else{ for(long long k = cache_n - 1; k > index; k--){ line[k].second = line[k].first + line[k+1].second; } dangerous += line[index + 1].second; } next[j] = index; } long long max_added = 0; for(auto num : next){ ++line[num].first; max_added = max(max_added, num); } cache_n = max_added + 1; } cout << dangerous << endl; }