1 #include <cstdio>
2 #include <cstring>
3 #include <cstdlib>
4 #include <cmath>
5 #include <deque>
6 #include <vector>
7 #include <queue>
8 #include <iostream>
9 #include <algorithm>
10 #include <map>
11 #include <set>
12 #include <ctime>
13 using namespace std;
14 typedef long long LL;
15 #define For(i, s, t) for(int i = (s); i <= (t); i++)
16 #define Ford(i, s, t) for(int i = (s); i >= (t); i--)
17 #define MIT (2147483647)
18 #define INF (1000000001)
19 #define MLL (1000000000000000001LL)
20 #define sz(x) ((bnt) (x).size())
21 #define clr(x, y) memset(x, y, sizeof(x))
22 #define puf push_front
23 #define pub push_back
24 #define pof pop_front
25 #define pob pop_back
26 #define ft first
27 #define sd second
28 #define mk make_pair
29 inline void SetIO(string Name) {
30 string Input = Name+".in",
31 Output = Name+".out";
32 freopen(Input.c_str(), "r", stdin),
33 freopen(Output.c_str(), "w", stdout);
34 }
35
36 const int N = 70;
37 int A, B, C, n, m, p, Data[N];
38 int Dp[2][N][N], Circle[N], Cir;
39 bool Visit[N];
40 int Ans;
41
42 inline void Input() {
43 scanf("%d%d%d%d%d", &A, &B, &C, &m, &p);
44 n = A+B+C;
45 }
46
47 inline void Plus(int &A, int B) {
48 A += B;
49 if(A >= p) A -= p;
50 }
51
52 inline void Multiply(int &A, int B) {
53 A = (((LL) A)*((LL) B))%p;
54 }
55
56 inline int Work() {
57 clr(Visit, 0), Cir = 0;
58 For(i, 1, n)
59 if(!Visit[i]) {
60 Circle[++Cir] = 0;
61 for(int x = i; !Visit[x]; x = Data[x]) {
62 Visit[x] = 1;
63 Circle[Cir]++;
64 }
65 }
66
67 int Now = 0, Next = 1, t, Sum = 0;
68 clr(Dp, 0);
69 Dp[Now][0][0] = 1;
70 For(s, 1, Cir) {
71 Sum += Circle[s];
72 clr(Dp[Next], 0);
73 For(i, 0, A)
74 For(j, 0, B)
75 if((t = Dp[Now][i][j]) > 0) {
76 if(i+Circle[s] <= A)
77 Plus(Dp[Next][i+Circle[s]][j], t);
78 if(j+Circle[s] <= B)
79 Plus(Dp[Next][i][j+Circle[s]], t);
80 if(Sum-i-j <= C)
81 Plus(Dp[Next][i][j], t);
82 }
83 Now ^= 1, Next ^= 1;
84 }
85
86 return Dp[Now][A][B];
87 }
88
89 inline int Ext_Gcd(int a, int b, int &x, int &y) {
90 if(!b) {
91 x = 1, y = 0;
92 return a;
93 } else {
94 int t, tx, ty;
95 t = Ext_Gcd(b, a%b, tx, ty);
96 x = ty, y = tx-(a/b)*ty;
97 return t;
98 }
99 }
100
101 inline void Solve() {
102 Ans = 0;
103 int Ret;
104 For(i, 1, m) {
105 For(j, 1, n) scanf("%d", &Data[j]);
106 Ret = Work();
107 Plus(Ans, Ret);
108 }
109 For(i, 1, n) Data[i] = i;
110 Ret = Work();
111 Plus(Ans, Ret);
112
113 int Inv, t;
114 Ext_Gcd(m+1, p, Inv, t);
115 Inv = ((Inv%p)+p)%p;
116 Multiply(Ans, Inv);
117
118 printf("%d\n", Ans);
119 }
120
121 int main() {
122 SetIO("1004");
123 Input();
124 Solve();
125 return 0;
126 }