1 #include<algorithm>
2 #include<cstdio>
3 #include<cmath>
4 #include<cstring>
5 #include<iostream>
6 int f[25][25][25];
7 int n,b[105],st[105],id[105],sum[105],inv[205],m,p,sr,sb,sg,sz,g[205];
8 int dp(){
9 sz=0;
10 for (int i=1;i<=n;i++) b[i]=0;
11 for (int i=0;i<=n;i++) st[i]=id[i]=sum[i]=0;
12 sum[0]=0;
13 for (int i=1;i<=n;i++)
14 if (!b[i]){
15 int ans=0;
16 while (!b[g[i]]) i=g[i],b[i]=1,ans++;
17 st[++sz]=ans;sum[sz]=sum[sz-1]+ans;id[sum[sz]]=sz;
18 }
19 for (int i=0;i<=sr;i++)
20 for (int j=0;j<=sb;j++)
21 for (int k=0;k<=sg;k++)
22 f[i][j][k]=0;
23 f[0][0][0]=1;
24 for (int i=0;i<=sr;i++)
25 for (int j=0;j<=sb;j++)
26 for (int k=0;k<=sg;k++)
27 if (id[i+j+k]){
28 int v=st[id[i+j+k]];
29 if (i>=v) f[i][j][k]=(f[i][j][k]+f[i-v][j][k])%p;
30 if (j>=v) f[i][j][k]=(f[i][j][k]+f[i][j-v][k])%p;
31 if (k>=v) f[i][j][k]=(f[i][j][k]+f[i][j][k-v])%p;
32 }
33 return f[sr][sb][sg];
34 }
35 int main(){
36 scanf("%d%d%d%d%d",&sr,&sb,&sg,&m,&p);
37 n=sr+sb+sg;
38 inv[1]=1;
39 int ans=0;
40 for (int i=2;i<p;i++)
41 inv[i]=(((p-p/i)%p)*(inv[p%i]))%p;
42 inv[p]=1;
43 for (int i=1;i<=m;i++){
44 for (int j=1;j<=n;j++)
45 scanf("%d",&g[j]);
46 ans=(ans+dp())%p;
47 }
48 for (int i=1;i<=n;i++)
49 g[i]=i;
50 ans=(ans+dp())%p;
51 ans=(ans*inv[m+1])%p;
52 printf("%d\n",ans);
53 }