#include <bits/stdc++.h>
inline int in() {
  int x = 0;
  char c = getchar();
  bool f = false;
  while (c < '0' || c > '9') f |= c == '-', c = getchar();
  while (c >= '0' && c <= '9')
    x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
  return f ? -x : x;
}
const int N = 1e6 + 5;
int cnt[2][N], mul[2][N], n, m, k, mod;
inline int qpow(int base, int b) {
  int ret = 1;
  for (; b; b >>= 1, base = (long long)base * base % mod)
    if (b & 1) ret = (long long)ret * base % mod;
  return ret;
}
int main() {
  bool rev = false;
  n = in(), m = in(), k = in();
  if ((n & 1) != (m & 1)) {
    puts("0");
    return 0;
  }
  if (n < m) std::swap(n, m), rev = true;
  for (int i = 1; i <= n; ++i) mul[0][i] = 1;
  for (int i = 1; i <= m; ++i) mul[1][i] = 1;
  for (int i = 1, x, y, z; i <= k; ++i) {
    x = in(), y = in(), z = in();
    if (rev) std::swap(x, y);
    ++cnt[0][x], ++cnt[1][y];
    mul[0][x] *= z, mul[1][y] *= z;
  }
  mod = in();
  for (int i = 1; i <= n; ++i)
    if (cnt[0][i] == m && mul[0][i] == 1) {
      puts("0");
      return 0;
    }
  for (int i = 1; i <= m; ++i)
    if (cnt[1][i] == n && mul[1][i] == 1) {
      puts("0");
      return 0;
    }
  int res = 1;
  rev = true;
  for (int i = 1; i <= n; ++i) {
    if (!cnt[0][i] && rev) {
      rev = false;
      continue;
    }
    if (cnt[0][i] == m) continue;
    res = (long long)res * qpow(2, m - cnt[0][i] - 1) % mod;
  }
  printf("%d\n", res);
  return 0;
}
