1 #include <iostream>
2 #include <cstring>
3 #include <string>
4 #include <cstdio>
5 #include <cstdlib>
6 #include <cmath>
7 #include <algorithm>
8 #include <queue>
9 #include <stack>
10 #include <map>
11 #include <set>
12 #include <list>
13 #include <vector>
14 #include <ctime>
15 #include <functional>
16 #define pritnf printf
17 #define scafn scanf
18 #define For(i,j,k) for(int i=(j);i<=(k);(i)++)
19 #define Clear(a) memset(a,0,sizeof(a))
20 using namespace std;
21 typedef long long LL;
22 typedef unsigned int Uint;
23 const int INF=0x7fffffff;
24 //==============struct declaration==============
25
26 //==============var declaration=================
27 const int MAXN=30;
28 int sr,sb,sg,n,m,p,MOD,cnt=0;
29 int ans=0;
30 int tran[MAXN*5];
31 int f[MAXN][MAXN][MAXN];
32 bool vis[MAXN*5];
33 //==============function declaration============
34 int quickpow(int a,int Exp);
35 int inv(int a){return quickpow(a,MOD-2);}
36 int fact(int x){int ans=1;for(int i=1;i<=x;i++) ans=(ans*i)%MOD;return ans;}
37 //==============main code=======================
38 int main()
39 {
40 //#define FILE__
41 #ifdef FILE__
42 freopen("input.txt","r",stdin);
43 freopen("output.txt","w",stdout);
44 #endif
45 scanf("%d%d%d%d%d",&sr,&sb,&sg,&m,&MOD);n=sr+sb+sg;
46 for(int i=1;i<=m;i++){
47 for(int j=1;j<=n;j++)
48 scanf("%d",tran+j);
49 memset(vis,false,sizeof(vis));memset(f,0,sizeof(f));
50 f[0][0][0]=1;cnt=0;
51 for(int p=1;p<=n;p++){
52 if (vis[p]) continue;
53 cnt++;
54 int siz=0,x=p;
55 while (!vis[x]){
56 vis[x]=true;
57 x=tran[x];
58 siz++;
59 }//找出循环节
60 for(int i=sr;i>=0;i--)
61 for(int j=sb;j>=0;j--)
62 for(int k=sg;k>=0;k--){
63 if (i>=siz) f[i][j][k]+=f[i-siz][j][k];
64 if (j>=siz) f[i][j][k]+=f[i][j-siz][k];
65 if (k>=siz) f[i][j][k]+-f[i][j][k-siz];
66 f[i][j][k]%=MOD;
67 } //DP
68 }
69 ans=(ans+f[sr][sb][sg])%MOD;
70 }
71 ans=ans+fact(n)*inv(fact(sr))*inv(fact(sb))*inv(fact(sg));
72 printf("%d\n",(ans*inv(m+1))%MOD);
73 return 0;
74 }
75 //================fuction code====================
76 int quickpow(int a,int Exp){
77 if (Exp==0) return 1;
78 if (Exp==1) return a;
79 int temp=quickpow(a,Exp/2);
80 temp=(temp*temp)%MOD;
81 if (Exp&1) temp=(temp*a)%MOD;
82 return temp;
83 }