#include using namespace std; #define ll long long struct FT { vector s; FT(int n) : s(n) {} void update(int pos, ll dif) { for(;pos < s.size();pos |= pos + 1) s[pos] += dif; } ll query(int pos) { ll res = 0; for (;pos > 0; pos &= pos - 1) res += s[pos-1]; return res; } }; int main() { int n;cin>>n; ll ans = 0; FT fen(n+5); int ins = 0; for(int i = 0;i < n;i++){ int m; cin>>m; vector pos(m); for(auto &i : pos) cin >> i; sort(pos.begin(),pos.end()); for(int x : pos) { ans += ins - fen.query(x+1); fen.update(x,1); ins++; } } cout << ans << endl; }