#include <bits/stdc++.h>
using namespace std;
int Pow(int N, int Mod) {
  if (!N) return 1;
  int T = Pow(N / 2, Mod);
  T = (long long)T * T % Mod;
  if (N & 1) T = T * 2 % Mod;
  return T;
}
int main() {
  int M, N;
  scanf("%d%d", &M, &N);
  if ((M + N) & 1) {
    printf("0\n");
    return 0;
  }
  if (M == 1 || N == 1) {
    int K, Ans = 1;
    scanf("%d", &K);
    while (Ans && K--) {
      int X, Y, Z;
      scanf("%d%d%d", &X, &Y, &Z);
      Ans = (Z < 0);
    }
    printf("%d\n", Ans);
    return 0;
  }
  int K;
  scanf("%d", &K);
  static int A[1000][1000];
  memset(A, 0, sizeof(A));
  for (int i = 0; i < K; i++) {
    int X, Y, Z;
    scanf("%d%d%d", &X, &Y, &Z);
    A[X - 1][Y - 1] = Z;
  }
  bool Bad = false;
  for (int i = 0; i < M; i++) {
    int Sum = 1, Count = 0;
    for (int j = 0; j < N; j++) {
      if (A[i][j]) Count++;
      if (A[i][j] == -1) Sum = -Sum;
    }
    if (Count == N)
      if (Sum == -1)
        K--;
      else
        Bad = true;
  }
  for (int j = 0; j < N; j++) {
    int Sum = 1, Count = 0;
    for (int i = 0; i < M; i++) {
      if (A[i][j]) Count++;
      if (A[i][j] == -1) Sum = -Sum;
    }
    if (Count == M)
      if (Sum == -1)
        K--;
      else
        Bad = true;
  }
  int Mod;
  scanf("%d", &Mod);
  printf("%d\n", Bad ? 0 : Pow((M - 1) * (N - 1) - K, Mod));
  return 0;
}
