#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int idata=100+5;
int i,j,k;
int judge,flag;
vector<int> maxx,minn;
int n,m,t;
ll cnt,temp;
int main()
{
cin>>t;
for(i=1;i<=t;i++)
{
int themax=-inf;
int themin=inf;
judge=0;
cin>>n;
for(j=1;j<=n;j++)
{
cin>>m;
if(m>themin)
{
judge=1;
}
themax=max(m,themax);
themin=min(m,themin);
}
if(judge)
{
minn.push_back(-1);
maxx.push_back(inf);
}
else
{
minn.push_back(themin);
maxx.push_back(themax);
}
}
stable_sort(maxx.begin(),maxx.end());
for(i=0;i<minn.size();i++)
{
cnt+=(maxx.end()-upper_bound(maxx.begin(),maxx.end(),minn[i]));
}
cout<<cnt<<endl;
return 0;
}