#include<cstdio>
#include<cctype>
#include<cstring>
#include<algorithm>
#define rep(i,s,t) for(int i=s;i<=t;i++)
#define dwn(i,s,t) for(int i=s;i>=t;i--)
#define ren for(int i=first[x];i;i=next[i])
using namespace std;
const int BufferSize=1<<16;
char buffer[BufferSize],*head,*tail;
inline char Getchar() {
if(head==tail) {
int l=fread(buffer,1,BufferSize,stdin);
tail=(head=buffer)+l;
}
return *head++;
}
inline int read() {
int x=0,f=1;char c=Getchar();
for(;!isdigit(c);c=Getchar()) if(c==‘-‘) f=-1;
for(;isdigit(c);c=Getchar()) x=x*10+c-‘0‘;
return x*f;
}
typedef long long ll;
const int maxn=20;
const int mod=1000000007;
typedef ll Matrix[maxn][maxn];
void gcd(ll a,ll b,ll& x,ll& y) {
if(!b) x=1,y=0;
else gcd(b,a%b,y,x),y-=x*(a/b);
}
ll getinv(ll a) {
ll b=mod,x,y;gcd(a,b,x,y);
return (x+mod)%mod;
}
Matrix B;
ll gauss(Matrix A,int n) {
ll ans=1;
rep(i,0,n-1) {
int r=i;
rep(j,i+1,n-1) if(abs(A[r][i])<abs(A[j][i])) r=j;
if(r!=i) ans*=-1,swap(A[r],A[i]);
ll inv=getinv(A[i][i]);
rep(k,0,n-1) if(i!=k)
dwn(j,n-1,i) A[k][j]=(A[k][j]-A[k][i]*A[i][j]%mod*inv%mod+mod)%mod;
}
rep(i,0,n-1) (ans*=A[i][i])%=mod;
return (ans+mod)%mod;
}
struct Company {
int m,u[maxn*maxn],v[maxn*maxn];
}A[maxn];
int main() {
int n=read();
rep(i,0,n-2) dwn(j,A[i].m=read(),1) A[i].u[j]=read(),A[i].v[j]=read();
ll ans=0;
rep(S,0,(1<<n-1)-1) {
int cnt=0;
rep(i,0,n-1) rep(j,0,n-1) B[i][j]=0;
rep(i,0,n-2) if(S>>i&1) {
cnt++;
rep(j,1,A[i].m) {
int u=A[i].u[j]-1,v=A[i].v[j]-1;
B[u][u]++;B[v][v]++;B[u][v]--;B[v][u]--;
}
}
rep(i,0,n-1) rep(j,0,n-1) (B[i][j]+=mod)%=mod;
if(n-1-cnt&1) (ans-=gauss(B,n-1))%=mod;
else (ans+=gauss(B,n-1))%=mod;
}
printf("%lld\n",(ans+mod)%mod);
return 0;
}