#include <bits/stdc++.h>
using namespace std;
namespace yspm {
inline long long read() {
  long long res = 0, f = 1;
  char k;
  while (!isdigit(k = getchar())) {
    if (k == '-') f = -1;
  }
  while (isdigit(k)) {
    res = res * 10 + k - '0';
    k = getchar();
  }
  return res * f;
}
inline void swap(long long &x, long long &y) {
  long long t = x;
  x = y, y = t;
  return;
}
inline long long max(long long x, long long y) { return x < y ? y : x; }
inline long long min(long long x, long long y) { return x > y ? y : x; }
const long long N = 1010;
long long n, m, a[N][N], x, y, z, k, p;
inline long long ksm(long long x, long long y) {
  long long res = 1;
  for (; y; y >>= 1, (x *= x) %= p)
    if (y & 1) (res *= x) %= p;
  return res;
}
signed main() {
  n = read();
  m = read();
  k = read();
  for (long long i = 1; i <= k; ++i) {
    x = read();
    y = read();
    z = read();
    if (n < m) swap(x, y);
    a[x][y] = z;
  }
  if (n < m) swap(n, m);
  p = read();
  if ((n + m) & 1) {
    puts("0");
    return 0;
  }
  long long flag = 0, cnt = (n - 1) * (m - 1) - k;
  for (long long i = 1; i <= n; ++i) {
    long long num = 0, mul = 1;
    for (long long j = 1; j <= m; ++j) {
      if (a[i][j]) num++, mul *= a[i][j];
    }
    if (num == m) {
      if (mul != -1) {
        flag = 1;
        break;
      }
      cnt++;
    }
  }
  if (flag) {
    cout << 0 << endl;
  } else
    cout << ksm(2, cnt) << endl;
  return 0;
}
}  // namespace yspm
signed main() {
  yspm::main();
  return 0;
}
