#include using namespace std; using ll = long long; #define F first #define S second #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define sz(x) (int)(x).size() #define int long long const int MAXN = 2 * 1e5 + 1; int tree[4 * MAXN]; int f(int a, int b) { return a + b; } void update(int v, int tl, int tr, int pos, int val) { if (tl == tr) { tree[v] = tree[v] + val; return; } int tm = (tl + tr) / 2; if (pos <= tm) update(v * 2, tl, tm, pos, val); else update(v * 2 + 1, tm + 1, tr, pos, val); tree[v] = f(tree[v * 2], tree[v * 2 + 1]); } int get(int v, int tl, int tr, int l, int r) { if (l > r) return 0; if (tl == l && tr == r) return tree[v]; int tm = (tl + tr) / 2; return get(v * 2, tl, tm, l, min(tm, r)) + get(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r); } void solve() { int n; cin >> n; int ans = 0; for (int i = 0; i < n; i++) { int p; cin >> p; vector a; for (int i = 0; i < p; i++) { int w; cin >> w; a.push_back(w); ans += get(1, 0, n - 1, w + 1, n - 1); } for (auto w : a) { update(1, 0, n - 1, w, 1); } } // for (int i = 1; i < 4; i++) { // cout << tree[i] << ' '; // } // cout << endl; cout << ans << endl; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); solve(); return 0; }