#include <bits/stdc++.h>
using namespace std;
long long POW(int x, int p) {
  if (x <= 0) return 1;
  long long s = POW(x / 2, p);
  s = s * s % p;
  if (x & 1) s = s * 2 % p;
  return s;
}
int a[1010][1010], b[1010][1010];
int main() {
  int n, m, k, p;
  scanf("%d%d", &n, &m);
  scanf("%d", &k);
  if ((n & 1) != (m & 1)) {
    puts("0");
    return 0;
  }
  for (int i = 0; i < k; ++i) {
    int x, y, z;
    scanf("%d%d%d", &x, &y, &z);
    a[x][y] = b[x][y] = z;
  }
  scanf("%d", &p);
  bool noans = false;
  for (int i = 1; i <= n; ++i) {
    int x = 1;
    for (int j = 1; j <= m; ++j) x *= a[i][j];
    if (x != 0 && x != -1) noans = true;
  }
  for (int j = 1; j <= m; ++j) {
    int x = 1;
    for (int i = 1; i <= n; ++i) x *= a[i][j];
    if (x != 0 && x != -1) noans = true;
  }
  if (noans) {
    puts("0");
    return 0;
  }
  if (m < n) {
    memset(a, 0, sizeof a);
    for (int i = 1; i <= n; ++i)
      for (int j = 1; j <= m; ++j) a[j][i] = b[i][j];
    swap(m, n);
  }
  int ss = 0;
  for (int j = 1; j <= m; ++j) {
    int x = 0;
    for (int i = 1; i <= n; ++i)
      if (a[i][j] == 0) ++x;
    if (x > 0) ss += x - 1;
  }
  ss -= n - 1;
  int ans = POW(ss, p);
  printf("%d\n", ans % p);
}
