#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
#define x first
#define y second
#define bg begin()
#define ed end()
#define pb push_back
#define mp make_pair
#define sz(a) int((a).size())
#define R(i,n) for(int i(0);i<(n);++i)
#define L(i,n) for(int i((n)-1);~i;--i)
const int iinf=0x3f3f3f3f;
const ll linf=0x3f3f3f3f3f3f3f3f;
//Data
const int N=17,M=16,S=1<<M;
int n,m,sm,pc[S];
vector<pair<int,int>> e[M];
//Math
const int mod=1e9+7;
void fmod(int &x){x+=x>>31&mod;}
//Matrix
struct Matrix{
int a[N][N];
int* operator[](int i){return a[i];}
void clear(){R(i,n)R(j,n) a[i][j]=0;}
int det(int res=1,int j=-1){
R(i,n)if(i){
for(j=i;j<n;j++)if(a[j][i]) break;
if(j==n) return 0;
if(i!=j){
for(int t=i;t<n;t++)
swap(a[i][t],a[j][t]);
res=-res;
}
for(int j=i+1;j<n;j++)while(a[j][i]){
int d=a[j][i]/a[i][i];
for(int t=i;t<n;t++) fmod(a[j][t]-=1ll*a[i][t]*d%mod);
if(!a[j][i]) break;
for(int t=i;t<n;t++) swap(a[i][t],a[j][t]);
res=-res;
}
}
fmod(res);
R(i,n) i&&(res=1ll*res*a[i][i]%mod);
return res;
}
}mat;
//Main
int main(){
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin>>n,m=n-1,sm=1<<m;
R(i,m){
int x; cin>>x;
while(x--){
pair<int,int> p;
cin>>p.x>>p.y,--p.x,--p.y;
e[i].pb(p);
}
}
int ns=0;
R(s,sm){
pc[s]=pc[s>>1]+(s&1),mat.clear();
R(i,m)if(s>>i&1)
for(pair<int,int> p:e[i])
++mat[p.x][p.x],++mat[p.y][p.y],
--mat[p.x][p.y],--mat[p.y][p.x];
R(i,n)R(j,n) fmod(mat[i][j]);
if((m-pc[s])&1) fmod(ns-=mat.det());
else fmod(ns+=mat.det()-mod);
}
cout<<ns<<'\n';
return 0;
}