1 #include <bits/stdc++.h>
2 using namespace std;
3 int p, x[65][65], f[25][25][25], siz[65];
4 bool vis[65];
5
6 int qpow(int a, int b)
7 {
8 int ans = 1;
9 for(; b; b >>= 1, a = a * a % p)
10 if(b & 1) ans = ans * a % p;
11 return ans;
12 }
13
14 int main()
15 {
16 int sr, sg, sb, m, inv, ans = 0, tot;
17 cin >> sr >> sb >> sg >> m >> p;
18 for(int i = 1; i <= m; ++i)
19 for(int j = 1; j <= sr + sg + sb; ++j)
20 cin >> x[i][j];
21 for(int i = 1; i <= sr + sg + sb; ++i)
22 x[m + 1][i] = i;
23 for(int z = 1; z <= m + 1; ++z)
24 {
25 memset(f, 0, sizeof(f));
26 memset(vis, 0, sizeof(vis));
27 memset(siz, 0, sizeof(siz));
28 f[0][0][0] = 1, tot = 0;
29 for(int i = 1; i <= sr + sg + sb; ++i)
30 if(!vis[i])
31 {
32 ++tot;
33 for(int j = i; !vis[j]; j = x[z][j])
34 vis[j] = true, ++siz[tot];
35 }
36 for(int l = 1; l <= tot; ++l)
37 for(int i = sr; ~i; --i)
38 for(int j = sg; ~j; --j)
39 for(int k = sb; ~k; --k)
40 {
41 if(i >= siz[l])
42 f[i][j][k] = (f[i][j][k] + f[i - siz[l]][j][k]) % p;
43 if(j >= siz[l])
44 f[i][j][k] = (f[i][j][k] + f[i][j - siz[l]][k]) % p;
45 if(k >= siz[l])
46 f[i][j][k] = (f[i][j][k] + f[i][j][k - siz[l]]) % p;
47 }
48 ans = (ans + f[sr][sg][sb]) % p;
49 }
50 cout << ans * qpow(m + 1, p - 2) % p << endl;
51 return 0;
52 }