#include <bits/stdc++.h>
using namespace std;
int n, m, k, x[1001], y[1001], xx[1001], yy[1001], r, chk, p, ans = 1;
int main() {
  scanf("%d%d%d", &n, &m, &k);
  r = (n - 1) * (m - 1) - k;
  for (int i = 0, a, b, c; i < k; i++) {
    scanf("%d%d%d", &a, &b, &c);
    x[a]++;
    y[b]++;
    if (c == -1) xx[a] ^= 1, yy[b] ^= 1;
  }
  scanf("%d", &p);
  for (int i = 1; i <= m; i++)
    if (y[i] == n) {
      r++;
      chk |= (yy[i] ^ 1);
    }
  for (int i = 1; i <= n; i++)
    if (x[i] == m) {
      r++;
      chk |= (xx[i] ^ 1);
    }
  if (chk || ((n & 1) ^ (m & 1)))
    ans = 0;
  else {
    for (int i = 0; i < r; i++) ans *= 2, ans %= p;
  }
  printf("%d\n", ans);
  return 0;
}
