#include <bits/stdc++.h>
int N, M, K, S, P, a[1005], b[1005], c[1005], d[1005], ans = 1;
int main() {
  scanf("%d%d%d", &N, &M, &K), S = (N - 1) * (M - 1) - K;
  if ((N + M) & 1) return puts("0"), 0;
  for (int i = 1; i <= N || i <= M; i++) c[i] = d[i] = 1;
  for (int i = 1, x, y, z; i <= K; i++)
    scanf("%d%d%d", &x, &y, &z), a[x]++, b[y]++, c[x] *= z, d[y] *= z;
  scanf("%d", &P);
  for (int i = 1; i <= N; i++)
    if (a[i] == M)
      if (c[i] == 1)
        return puts("0"), 0;
      else
        S++;
  for (int i = 1; i <= M; i++)
    if (b[i] == N)
      if (d[i] == 1)
        return puts("0"), 0;
      else
        S++;
  for (int i = 1; i <= S; i++) (ans <<= 1) %= P;
  printf("%d\n", ans);
}
