#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void R(T &xx) {
  xx = 0;
  char ch = getchar();
  bool F = 0;
  while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
  if (ch == '-') F = 1, ch = getchar();
  while (ch >= '0' && ch <= '9')
    xx = xx + xx + (xx << 3) + ch - 48, ch = getchar();
  if (F) xx = -xx;
}
template <class T>
inline void add(int &x, T y) {
  for (x += y; x >= 51123987; x -= 51123987)
    ;
}
bool a[4011], b[4011];
int n, m, k, p, hsx[4011], hsy[4011];
int main() {
  R(n), R(m), R(k);
  if (n + m & 1) return puts("0"), 0;
  int g = (n - 1) * (m - 1) - k;
  int x, y, z;
  for (int i = 0; i < (int)k; i++) {
    R(x), R(y), R(z);
    z = (z < 0);
    a[x] ^= z, b[y] ^= z;
    if ((++hsx[x]) == m)
      if (a[x])
        g++;
      else
        return puts("0"), 0;
    if ((++hsy[y]) == n)
      if (b[y])
        g++;
      else
        return puts("0"), 0;
  }
  R(p);
  int ans = 1;
  for (int i = 0; i < (int)g; i++) (ans <<= 1) %= p;
  printf("%d\n", ans);
  return 0;
}
