1 #include <cstdio>
2 #include <cstring>
3 int Sr, Sb, Sg, n, m, p, a[100][100];
4
5 int pow_mod(int a, int k, int m) {
6 if (k == 0) return 1;
7 if (k == 1) return a % m;
8 int ret = pow_mod(a, k / 2, m);
9 ret = ret * ret % m;
10 if (k % 2 == 1) ret = ret * a % m;
11 return ret;
12 }
13
14 int inv(int a, int m) {
15 return pow_mod(a, m - 2, m);
16 }
17
18 void init() {
19 scanf("%d%d%d%d%d", &Sr, &Sb, &Sg, &m, &p);
20 n = Sr + Sb + Sg;
21 for (int i = 1; i <= m; i++)
22 for (int j = 1; j <= n; j++) scanf("%d", &a[i][j]);
23 m++;
24 for (int i = 1; i <= n; i++) a[m][i] = i;
25 }
26
27 int compute(int t) {
28 int N = 0, arr[100], f[50][50][50];
29 bool b[100]; memset(b, 0, sizeof(b));
30 for (int i = 1; i <= n; i++)
31 if (!b[i]) {
32 int pos = i, cnt = 0;
33 while (!b[pos]) {
34 b[pos] = 1; cnt++;
35 pos = a[t][pos];
36 }
37 arr[++N] = cnt;
38 }
39 memset(f, 0, sizeof(f));
40 int sum = 0;
41 f[0][0][0] = 1;
42 for (int i = 1; i <= N; i++) {
43 sum += arr[i];
44 for (int i1 = Sr; i1 >= 0; i1--)
45 for (int i2 = Sb; i2 >= 0; i2--)
46 for (int i3 = Sg; i3 >= 0; i3--)
47 if (i1 + i2 + i3 == sum) {
48 if (i1 >= arr[i])
49 f[i1][i2][i3] = (f[i1][i2][i3] + f[i1 - arr[i]][i2][i3]) % p;
50 if (i2 >= arr[i])
51 f[i1][i2][i3] = (f[i1][i2][i3] + f[i1][i2 - arr[i]][i3]) % p;
52 if (i3 >= arr[i])
53 f[i1][i2][i3] = (f[i1][i2][i3] + f[i1][i2][i3 - arr[i]]) % p;
54 }
55 }
56 return f[Sr][Sb][Sg];
57 }
58
59 int main() {
60 //freopen("input.txt", "r", stdin);
61 //freopen("output.txt", "w", stdout);
62 init();
63 int ans = 0;
64 for (int i = 1; i <= m; i++)
65 ans = (ans + compute(i)) % p;
66 ans = (ans * inv(m, p)) % p;
67 printf("%d\n", ans);
68 return 0;
69 }