#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; next.reserve(birds); for(long long j = 0; j < birds; j++){ long long index; cin >> index; if (index + 1 < cache_n){ for(long long k = cache_n - 1; k > index; k--){ line[k].second = line[k].first + line[k+1].second; } cache_n = index + 1; } dangerous += line[index + 1].second; next.push_back(index); } for(auto num : next){ line[num].first += 1; cache_n = max(cache_n, num + 1); } } cout << dangerous << endl; }