inlinevoidAdd(int &x, int y){ if ((x += y) >= mod) x -= mod; }
int n, m; int sr, sg, sb; int f[$s][$s][$s]; int ind[$n], av[$n]; inlineintksm(int x, int p = mod - 2){ int ret = 1; for (; p; p >>= 1, (x *= x) %= mod) if (p & 1) (ret *= x) %= mod; return ret; } inlineintpurge(int x){ int cnt = 0; while (true) { constint tar = ind[x]; if (!tar) break; ind[x] = 0; x = tar; ++cnt; } return cnt; } inlineintstable(){ int tot = 0; for (int i = 1; i <= n; ++i) if (ind[i]) av[++tot] = purge(i); f[0][0][0] = 1; for (int q = 1; q <= tot; ++q) { constint cur = av[q]; for (int i = sr; ~i; --i) for (int j = sg; ~j; --j) for (int k = sb; ~k; --k) f[i][j][k] = (((i >= cur) ? f[i - cur][j][k] : 0) + ((j >= cur) ? f[i][j - cur][k] : 0) + ((k >= cur) ? f[i][j][k - cur] : 0)) % mod; } return f[sr][sg][sb]; } intmain(){ cin > sr > sg > sb > m > mod; n = sr + sg + sb;
for (int i = 1; i <= n; ++i) ind[i] = i; int ans = stable(); constint invm = ksm(m + 1); while (m--) { for (int i = 1; i <= n; ++i) cin > ind[i]; Add(ans, stable()); } ans = ans * invm % mod; cout < ans < endl; }