#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
int n, m, k, g, op, ans = 1;
int a[N], b[N], A[N], B[N];
int main() {
  scanf("%d%d%d", &n, &m, &k);
  g = (n - 1) * (m - 1) - k;
  if ((m ^ n) & 1) return puts("0"), 0;
  for (int i = 1, x, y, z; i <= k; ++i) {
    scanf("%d%d%d", &x, &y, &z);
    a[x] ^= (z == -1);
    b[y] ^= (z == -1);
    if ((++A[x]) == m)
      if (a[x] == 0)
        return puts("0"), 0;
      else
        ++g;
    if ((++B[y]) == n)
      if (b[y] == 0)
        return puts("0"), 0;
      else
        ++g;
  }
  scanf("%d", &op);
  for (int j = 2; g > 0; g >>= 1) {
    if (g & 1) ans = (long long)ans * j % op;
    j = (long long)j * j % op;
  }
  printf("%d\n", ans);
  return 0;
}
