Problem:
Score : 2000 points

Problem Statement

For each of the K^{NM} ways to write an integer between 1 and K (inclusive) in every square in a square grid with N rows and M columns, find the value defined below, then compute the sum of all those K^{NM} values, modulo D.

For each of the NM squares, find the minimum among the N+M-1 integers written in the square's row or the square's column. The value defined for the grid is the product of all these NM values.

Constraints

1 \leq N,M,K \leq 100

10^8 \leq D \leq 10^9

N,M,K, and D are integers.

D is prime.

Input

Input is given from Standard Input in the following format:

N M K D

Output

Print the sum of the K^{NM} values, modulo D.

Sample Input 1

2 2 2 998244353

Sample Output 1

35

We have 1 way to write integers such that the product of the NM values is 16, 4 ways such that the product is 2, and 11 ways such that the product is 1.

Sample Input 2

2 3 4 998244353

Sample Output 2

127090

Sample Input 3

31 41 59 998244353

Sample Output 3

827794103

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include<bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define uint unsigned
#define pii pair<int,int>
#define pll pair<ll,ll>
#define PB push_back
#define fi first
#define se second
#define For(i,j,k) for (int i=(int)(j);i<=(int)(k);i++)
#define Rep(i,j,k) for (int i=(int)(j);i>=(int)(k);i--)
#define CLR(a,v) memset(a,v,sizeof(a));
#define CPY(a,b) memcpy(a,b,sizeof(a));
using namespace std;
const int N=105;
int n,m,K,mo;
int C[N][N],f[N][N],g[N][N];
int P[N][N*N],IP[N][N*N];
int power(int x,int y){
	int s=1;
	for (;y;y/=2,x=1ll*x*x%mo)
		if (y&1) s=1ll*s*x%mo;
	return s; 
}
int main(){
	scanf("%d%d%d%d",&n,&m,&K,&mo);
	For(i,0,N-1) C[i][0]=1;
	For(i,0,K) P[i][0]=IP[i][0]=1;
	For(i,0,K) IP[i][1]=power(i,mo-2);
	For(i,0,K) For(j,1,n*m) P[i][j]=1ll*P[i][j-1]*i%mo;
	For(i,0,K) For(j,1,n*m) IP[i][j]=1ll*IP[i][j-1]*IP[i][1]%mo;
	For(i,1,N-1) For(j,1,i)
		C[i][j]=(C[i-1][j-1]+C[i-1][j])%mo;
	f[0][0]=1;
	For(i,1,K){
		For(j,0,n) For(k,0,m) f[j][k]=1ll*f[j][k]*P[i][(n-j)*(m-k)]%mo*IP[K-i+1][j*k]%mo;
		memset(g,0,sizeof(g));
		For(j,0,n) For(k,0,m) For(jj,0,n-j)
			g[j+jj][k]=(g[j+jj][k]+1ll*f[j][k]*C[jj+j][j])%mo;
		memset(f,0,sizeof(f));
		For(j,0,n) For(k,0,m) For(kk,0,m-k)
			f[j][k+kk]=(f[j][k+kk]+1ll*g[j][k]*C[kk+k][k])%mo;
		For(j,0,n) For(k,0,m) f[j][k]=1ll*f[j][k]*P[K-i+1][j*k]%mo;
		if (K!=i){
			For(j,0,n) For(k,0,m) f[j][k]=1ll*f[j][k]*IP[K-i][j*k]%mo;
			memset(g,0,sizeof(g));
			For(j,0,n) For(k,0,m) For(jj,0,n-j)
				g[j+jj][k]=(g[j+jj][k]+1ll*f[j][k]*(jj&1?mo-C[jj+j][j]:C[jj+j][j]))%mo;
			memset(f,0,sizeof(f));
			For(j,0,n) For(k,0,m) For(kk,0,m-k)
				f[j][k+kk]=(f[j][k+kk]+1ll*g[j][k]*(kk&1?mo-C[kk+k][k]:C[kk+k][k]))%mo;
			For(j,0,n) For(k,0,m) f[j][k]=1ll*f[j][k]*IP[i][(n-j)*(m-k)]%mo*P[K-i][j*k]%mo;
		}
	}
	printf("%d",f[n][m]);
}