#include <bits/stdc++.h>
using namespace std;
int n, m, K, MOD;
int val[2][1005], num[2][1005];
int main() {
  scanf("%d%d%d", &n, &m, &K);
  if ((n & 1) != (m & 1)) {
    printf("0\n");
    return 0;
  }
  int tol = (n - 1) * (m - 1) - K;
  for (int i = 1; i <= max(n, m); i++) val[0][i] = val[1][i] = 1;
  while (K--) {
    int x, y, c;
    scanf("%d%d%d", &x, &y, &c);
    num[0][x]++, num[1][y]++;
    val[0][x] *= c;
    val[1][y] *= c;
  }
  for (int i = 1; i <= n; i++)
    if (num[0][i] == m) {
      if (val[0][i] == -1)
        tol++;
      else {
        printf("0\n");
        return 0;
      }
    }
  for (int i = 1; i <= m; i++)
    if (num[1][i] == n) {
      if (val[1][i] == -1)
        tol++;
      else {
        printf("0\n");
        return 0;
      }
    }
  scanf("%d", &MOD);
  int ans = 1;
  while (tol--) ans = ans * 2 % MOD;
  printf("%d\n", ans);
  return 0;
}
