#include <bits/stdc++.h>
using namespace std;
long long cc;
int N, M, K, x, y, t, p;
int f1[2000], f2[2000], s1[2000], s2[2000], l[2000];
bool b[1010][1010];
long long quick(int p) {
  long long s = 1;
  long long x = 2;
  for (; p; p /= 2) {
    if (p & 1) s = s * x % cc;
    x = x * x % cc;
  }
  return s;
}
int main() {
  scanf("%d%d%d", &N, &M, &K);
  for (int i = 1; i <= max(N, M); i++) s1[i] = 1, s2[i] = 1, l[i] = M;
  for (int i = 1; i <= K; i++) {
    scanf("%d%d%d", &x, &y, &t);
    b[x][y] = true;
    if (l[x] && b[x][l[x]]) l[x]--;
    s1[x] *= t;
    s2[y] *= t;
    f1[x]++;
    f2[y]++;
  }
  scanf("%I64d", &cc);
  for (int i = 1; i <= N; i++)
    if (f1[i] == M && s1[i] == 1) {
      printf("0");
      return 0;
    }
  for (int i = 1; i <= M; i++)
    if (f2[i] == N && s2[i] == 1) {
      printf("0");
      return 0;
    }
  if ((N + M) & 1) {
    printf("0");
    return 0;
  }
  p = 0;
  for (int i = 1; i <= N; i++)
    if (f1[i] < M) {
      f2[l[i]]++;
      p += M - f1[i] - 1;
    }
  for (int i = 1; i <= M; i++)
    if (f2[i] != N) p--;
  printf("%I64d", quick(p));
}
