#include <bits/stdc++.h>
using namespace std;
int n, m, k, a, b, c, p;
int mas[1010][1010];
int main() {
  scanf("%d %d %d", &n, &m, &k);
  for (int i = 0; i < k; i++) {
    scanf("%d %d %d", &a, &b, &c);
    mas[a - 1][b - 1] = c;
  }
  scanf("%d", &p);
  if (n % 2 != m % 2) {
    printf("0");
    return 0;
  }
  for (int i = 0; i < n; i++) {
    int tmp = 1;
    int fl = 0;
    for (int j = 0; j < m; j++) {
      if (mas[i][j] == 0) {
        fl = 1;
        break;
      } else
        tmp *= mas[i][j];
    }
    if (fl) continue;
    if (tmp != -1) {
      printf("0");
      return 0;
    } else
      k--;
  }
  for (int j = 0; j < m; j++) {
    int tmp = 1;
    int fl = 0;
    for (int i = 0; i < n; i++) {
      if (mas[i][j] == 0) {
        fl = 1;
        break;
      } else
        tmp *= mas[i][j];
    }
    if (fl) continue;
    if (tmp != -1) {
      printf("0");
      return 0;
    } else
      k--;
  }
  int st = (n - 1) * (m - 1) - k;
  if (st < 0) {
    printf("0");
    return 0;
  }
  long long a = 2;
  long long res = 1;
  while (st) {
    if (st & 1) res = (res * a) % p;
    a = (a * a) % p;
    st >>= 1;
  }
  printf("%lld", res);
  return 0;
}
