#include <bits/stdc++.h>
using namespace std;
const long long N = 1010;
long long n, x, y, v, cnt[N], multi[N], m, k, mod;
long long pow2[N];
long long read() {
  long long x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = (x << 1) + (x << 3) + (c ^ 48);
    c = getchar();
  }
  return x * f;
}
signed main() {
  n = read(), m = read();
  bool cge = false;
  if (n < m) swap(n, m), cge = true;
  k = read();
  for (long long i = 1; i <= n; i++) multi[i] = 1;
  for (long long i = 1; i <= k; i++) {
    x = read(), y = read(), v = read();
    if (cge) swap(x, y);
    cnt[x]++;
    multi[x] *= v;
  }
  mod = read();
  if ((n & 1) != (m & 1)) puts("0"), exit(0);
  for (long long i = 1; i < n; i++) {
    if (!cnt[i]) {
      swap(cnt[i], cnt[n]);
      swap(multi[i], multi[n]);
      break;
    }
  }
  pow2[0] = 1;
  for (long long i = 1; i <= n; i++) pow2[i] = (pow2[i - 1] * 2) % mod;
  long long ans = 1;
  for (long long i = 1; i < n; i++) {
    if (!ans) break;
    if (cnt[i] == m) {
      if (multi[i] == 1) ans = 0;
    } else
      ans = ans * pow2[m - cnt[i] - 1] % mod;
  }
  cout << ans << endl;
  return 0;
}
