1 /**************************************************************
2 Problem: 1004
3 User: 935671154
4 Language: C++
5 Result: Accepted
6 Time:24 ms
7 Memory:1396 kb
8 ****************************************************************/
9
10 #include <iostream>
11 #include <cctype>
12 #include <cstdio>
13 #include <vector>
14 #include <algorithm>
15 #include <cmath>
16 #include <queue>
17 using namespace std;
18 inline void getd(int &x){
19 char c = getchar();
20 bool minus = 0;
21 while(!isdigit(c) && c != ‘-‘)c = getchar();
22 if(c == ‘-‘)minus = 1, c = getchar();
23 x = c - ‘0‘;
24 while(isdigit(c = getchar()))x = x * 10 + c - ‘0‘;
25 if(minus)x = -x;
26 }
27 /*======================================================*/
28 inline void exgcd(int a, int b, int &x, int &y){
29 if(!a){x = 0, y = 1; return;}
30 exgcd(b % a, a, y, x);
31 x -= b / a * y;
32 }
33
34 const int maxn = 63, maxk = 22;
35 int R, B, G, m, mod, ans, inv, n;
36 int p[maxn], loop[maxn], len;
37 int dp[maxn][maxk][maxk] = {0};
38 int fact[maxn];
39
40 inline void init(){
41 getd(R), getd(B), getd(G), getd(m), getd(mod);
42 n = R + B + G;
43 int i;fact[0] = 1;
44 for(i = 1;i <= n;++i) fact[i] = fact[i-1] * i % mod;
45 int x, y, t; ans = fact[n];
46 t = fact[R] * fact[B] * fact[G] % mod;
47 exgcd(t, mod, x, y);
48 ans = ans * x % mod;
49 if(ans < 0) ans += mod;
50 exgcd(m + 1, mod, x, y);
51 inv = x < 0 ? x + mod : x;
52 }
53
54 inline void analy(){
55 len = 0;
56 bool used[maxn] = {0};
57 int i, j, cnt;
58 for(i = 1;i <= n;++i){
59 if(!used[i]){
60 used[i] = 1, cnt = 1, j = p[i];
61 while(j != i){
62 used[j] = 1; ++cnt;
63 j = p[j];
64 }
65 loop[++len] = cnt;
66 }
67 }
68 }
69
70 inline int count(){
71 int i, j, k, s = 0;
72 dp[0][0][0] = 1;
73 for(i = 1;i <= len;++i){
74 s += loop[i];
75 for(j = 0;j <= R;++j) for(k = 0;k <= B;++k){
76 dp[i][j][k] = 0;
77 if(j >= loop[i])dp[i][j][k] += dp[i-1][j-loop[i]][k];
78 if(k >= loop[i])dp[i][j][k] += dp[i-1][j][k-loop[i]];
79 if(s-i-j >= loop[i])dp[i][j][k] += dp[i-1][j][k];
80 dp[i][j][k] %= mod;
81 }
82 }
83 return dp[len][R][B] % mod;
84 }
85
86 inline void work(){
87 int i;
88 while(m--){
89 for(i = 1;i <= n;++i) getd(p[i]);
90 analy();
91 ans = (ans + count()) % mod;
92 }
93 printf("%d\n", ans * inv % mod);
94 }
95
96 int main(){
97 #if defined DEBUG
98 freopen("test", "r", stdin);
99 #endif
100
101 init();
102
103 work();
104
105 #if defined DEBUG
106 cout << endl << (double)clock()/CLOCKS_PER_SEC << endl;
107 #endif
108 return 0;
109 }