#include <bits/stdc++.h>
using namespace std;
inline long long in() {
  int32_t x;
  scanf("%d", &x);
  return x;
}
inline long long lin() {
  long long x;
  scanf("%lld", &x);
  return x;
}
const long long maxn = 1e3 + 10;
const long long inf = 1e18 + 10;
const long long mod = 1e9 + 7;
long long res = 1;
bool changed = false;
long long z[maxn];
long long cnt[maxn];
int32_t main() {
  long long n = in(), m = in();
  if ((n + m) % 2) return cout << 0 << endl, 0;
  if (n < m) swap(n, m), changed = true;
  long long k = in();
  fill(z, z + maxn, 1);
  for (long long i = 0; i < k; i++) {
    long long x = in() - 1, y = in() - 1, value = in();
    if (changed) swap(x, y);
    z[x] *= value;
    cnt[x]++;
  }
  bool vis = 0;
  long long p = in();
  for (long long i = 0; i < n; i++) {
    if (!vis && cnt[i] == 0) {
      vis = true;
      continue;
    }
    if (cnt[i] == m && z[i] == 1) return cout << 0 << endl, 0;
    for (long long pt = 0; pt < m - cnt[i] - 1; pt++) res = res * 2 % p;
  }
  assert(vis);
  cout << res << endl;
}
