#include <bits/stdc++.h>
using namespace std;
int hang[1010], hf[1010];
int main() {
  int n, m, k, x, y, z, ans = 0, i, j, mod;
  bool bk = false;
  scanf("%d%d", &n, &m);
  if (n < m) {
    bk = true;
    int t = n;
    n = m;
    m = t;
  }
  scanf("%d", &k);
  for (i = 1; i <= n; i++) hf[i] = 1;
  for (i = 1; i <= k; i++) {
    scanf("%d%d%d", &x, &y, &z);
    if (bk) {
      int t = x;
      x = y;
      y = t;
    }
    hang[x]++;
    hf[x] *= z;
  }
  if (n % 2 != m % 2) {
    printf("0\n");
    return 0;
  }
  scanf("%d", &mod);
  bk = false;
  ans = 1;
  for (i = 1; i <= n; i++)
    if (hang[i] == 0 && !bk) {
      bk = true;
    } else if (hang[i] == m && hf[i] == 1) {
      ans = 0;
      break;
    } else {
      for (j = 1; j <= m - hang[i] - 1; j++) ans = ans * 2 % mod;
    }
  printf("%d\n", ans);
  return 0;
}
