#include <bits/stdc++.h>
using std::swap;
const int MAXN = 1010;
int mat[MAXN][MAXN];
int n, m, k, p;
int quick_pow(int a, int b) {
  int ans = 1, base = a;
  while (b) {
    if (b & 1) ans = ((long long)ans * base) % p;
    base = ((long long)base * base) % p;
    b >>= 1;
  }
  return ans;
}
int main() {
  scanf("%d%d%d", &n, &m, &k);
  int in1, in2, in3;
  for (int i = 1; i <= k; ++i) {
    scanf("%d%d%d", &in1, &in2, &in3);
    if (n < m) swap(in1, in2);
    mat[in1][in2] = in3;
  }
  scanf("%d", &p);
  if (n < m) swap(n, m);
  if ((n - m) & 1) {
    printf("0");
    return 0;
  }
  int tot = (n - 1) * (m - 1) - k;
  for (int i = 1; i <= n; ++i) {
    int ans = 1, cnt = 0;
    for (int j = 1; j <= m; ++j) {
      if (mat[i][j]) {
        ++cnt;
        ans *= mat[i][j];
      }
    }
    if (cnt == m) {
      if (ans == -1)
        ++tot;
      else if (ans == 1) {
        printf("0");
        return 0;
      }
    }
  }
  printf("%d", quick_pow(2, tot));
  return 0;
}
