#include <bits/stdc++.h>
using namespace std;
long long p;
long long n, m, k;
long long tag = 0;
long long cnt[100000];
long long sum[100000];
inline long long read() {
  long long x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 1) + (x << 3) + (ch ^ 48);
    ch = getchar();
  }
  return x * f;
}
long long ksm(long long x, long long y) {
  long long ans = 1;
  while (y) {
    if (y & 1) ans = (ans * x) % p;
    x = (x * x) % p;
    y >>= 1;
  }
  return ans;
}
signed main() {
  n = read(), m = read(), k = read();
  if (n < m) swap(n, m), tag = 1;
  for (long long i = 1; i <= n; i++) sum[i] = 1;
  for (long long i = 1; i <= k; i++) {
    long long x = read(), y = read(), z = read();
    if (tag) swap(x, y);
    cnt[x]++;
    sum[x] *= z;
  }
  p = read();
  if ((n + m) & 1) {
    cout << 0 << endl;
    return 0;
  }
  long long ans = 0;
  for (long long i = 1; i <= n; i++) {
    if (cnt[i] == m) {
      if (sum[i] == 1) {
        cout << 0 << endl;
        return 0;
      }
      continue;
    }
    ans += m - cnt[i] - 1;
  }
  ans -= m - 1;
  cout << ksm(2, ans);
  return 0;
}
