#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 1010;
long long mul[MAXN], def[MAXN];
long long qpow(long long base, long long p, long long mod) {
  long long res = 1;
  while (p) {
    if (p & 1) res = (res * base) % mod;
    base = (base * base) % mod;
    p >>= 1;
  }
  return res;
}
int main() {
  long long n, m, k, i, j, mod;
  bool f = false;
  scanf("%lld %lld %lld", &n, &m, &k);
  if (n < m) {
    swap(n, m);
    f = true;
  }
  for (i = 1; i <= n; i++) mul[i] = 1;
  for (i = 1; i <= k; i++) {
    long long x, y, num;
    scanf("%lld %lld %lld", &x, &y, &num);
    if (f) swap(x, y);
    mul[x] *= num;
    def[x]++;
  }
  scanf("%lld", &mod);
  if (n % 2 != m % 2) {
    printf("0");
    return 0;
  }
  long long p = -1, res = 1;
  for (i = 1; i <= n; i++) {
    if (!def[i]) {
      p = i;
      break;
    }
  }
  for (i = 1; i <= n; i++) {
    if (i == p) continue;
    if (def[i] == m && ~mul[i]) {
      printf("0");
      return 0;
    } else if (def[i] != m)
      res = (res * qpow(2, m - def[i] - 1, mod)) % mod;
  }
  printf("%lld", res);
  return 0;
}
