#include using namespace std; #define int long long int n; int t[1010101]; void upd(int v, int vl, int vr, int pos, int val) { if (vl == vr) { t[v] += val; return; } int vm = (vl + vr) / 2; if (pos <= vm) upd(2 * v, vl, vm, pos, val); else upd(2 * v + 1, vm + 1, vr, pos, val); t[v] = t[2 * v] + t[2 * v + 1]; } int gt(int v, int vl, int vr, int l, int r) { if (l <= vl && vr <= r) return t[v]; if (r < vl || vr < l) return 0; int vm = (vl + vr) / 2; return gt(2 * v, vl, vm, l, r) + gt(2 * v + 1, vm + 1, vr, l, r); } int ans = 0; int32_t main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n; for (int id = 1; id <= n; id++) { int p; cin >> p; vector a(p); for (int i = 1; i <= p; i++) { cin >> a[i]; a[i]++; } for (int i = 1; i <= p; i++) { if (a[i] == n) continue; ans += gt(1, 1, n, a[i] + 1, n); } for (int i = 1; i <= p; i++) { upd(1, 1, n, a[i], 1); } } cout << ans << '\n'; return 0; }