import java.io.*;
import java.util.*;
public class TreeMovingDiv2
{
static long mod=(long)(1e9+7);
static ArrayList<Pair>[] al;
static long[][][] dp;
static int maxn=55;
static ArrayList<Integer>[][] tree;
static List<Integer> list;
static boolean[] v;
static void dfs(int tree_num,int u,Pair p1)
{
v[u]=true;list.add(u);
for(int x:tree[tree_num][u])
{
if(!v[x])
{
if((p1.u==u && p1.v==x) || (p1.u==x && p1.v==u))
{
continue;
}
dfs(tree_num,x,p1);
}
}
}
static boolean check(int tree_no,Pair p1,Pair p2,int n)
{
v=new boolean[n];
for(int i=0;i<n;i++)
{
if(!v[i])
{
list=new ArrayList<Integer>();dfs(tree_no,i,p1);
{
return false;
}
break;
}
}
return true;
}
static long solve(int i,int j,int k,int m,int n)
{
if(i==m-1)
{
if(check(m-1,al[m-1].get(k),al[m-2].get(j),n))
{
return 1;
}
return 0;
}
if(dp[i][j][k]!=-1)
{
return dp[i][j][k];
}
else
{
long ret=0;
for(int x=0;x<=n-2;x++)
{
int prev=(i==0?m-1:i-1);
Pair curr1=al[i].get(x),curr2=al[prev].get(j);
if(check(i,al[i].get(x),curr2,n))
{
ret=(ret+solve(i+1,x,k,m,n))%mod;
}
}
dp[i][j][k]=ret;return ret;
}
}
@SuppressWarnings("unchecked")
public static int count(int n,int[] roots,int[] a,int[] b,int[] c)
{
for(int i=0;i<m;i++)
{
al[i]=new ArrayList<Pair>();
}
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
tree[i][j]=new ArrayList<Integer>();
}
}
for(int i=0;i<m;i++)
{
long[] x=new long[n-1];x[0]=c[i];
for(int j=1;j<=n-2;j++)
{
long curr=(a[i]*x[j-1]+b[i])%mod;
}
for(int j=0;j<=n-2;j++)
{
long curr=roots[i]+j+1;curr%=n;int u=(int)curr;
curr=x[j]%(j+1);curr=(curr+roots[i])%n;int v=(int) curr;
al[i].add(new Pair(u,v));
tree[i][u].add(v);tree[i][v].add(u);
}
}
dp=new long[55][55][55];
for(int i=0;i<maxn;i++)
{
for(int j=0;j<maxn;j++)
{
for(int k=0;k<maxn;k++)
{
dp[i][j][k]=-1;
}
}
}
long ret=0;
for(int i=0;i<=n-2;i++)
{
ret=(ret+solve(0,i,i,m,n))%mod;
}
return (int)ret;
}
{
//out.println(count(3,new int[]{0, 2},new int[]{1, 2},new int[]{1, 0},new int[]{3, 5}));
out.close();
}
}
class Pair
{
int u,v;
public Pair(int u,int v)
{
this.u=u;this.v=v;
}
}
aW1wb3J0IGphdmEuaW8uKjsKaW1wb3J0IGphdmEudXRpbC4qOwpwdWJsaWMgY2xhc3MgVHJlZU1vdmluZ0RpdjIKewogICAJc3RhdGljIFByaW50V3JpdGVyIG91dD1uZXcgUHJpbnRXcml0ZXIoU3lzdGVtLm91dCk7CglzdGF0aWMgbG9uZyBtb2Q9KGxvbmcpKDFlOSs3KTsKCXN0YXRpYyBBcnJheUxpc3Q8UGFpcj5bXSBhbDsKCXN0YXRpYyBsb25nW11bXVtdIGRwOwoJc3RhdGljIGludCBtYXhuPTU1OwoJc3RhdGljIEFycmF5TGlzdDxJbnRlZ2VyPltdW10gdHJlZTsKCXN0YXRpYyBMaXN0PEludGVnZXI+IGxpc3Q7CglzdGF0aWMgYm9vbGVhbltdIHY7CgkKCXN0YXRpYyB2b2lkIGRmcyhpbnQgdHJlZV9udW0saW50IHUsUGFpciBwMSkKCXsKCQl2W3VdPXRydWU7bGlzdC5hZGQodSk7CgkJCgkJZm9yKGludCB4OnRyZWVbdHJlZV9udW1dW3VdKQoJCXsKCQkJaWYoIXZbeF0pCgkJCXsKCQkJCWlmKChwMS51PT11ICYmIHAxLnY9PXgpIHx8IChwMS51PT14ICYmIHAxLnY9PXUpKQoJCQkJewoJCQkJCWNvbnRpbnVlOwoJCQkJfQoJCQkJCgkJCQlkZnModHJlZV9udW0seCxwMSk7CgkJCX0KCQl9Cgl9CgoJc3RhdGljIGJvb2xlYW4gY2hlY2soaW50IHRyZWVfbm8sUGFpciBwMSxQYWlyIHAyLGludCBuKQoJewoJCXY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