#include <bits/stdc++.h>
using namespace std;
long long f[1001][1001];
int m, n, T;
int x[1001], y[1001];
long long p;
bool ok;
int main() {
  scanf("%d%d%d", &n, &m, &T);
  if (m > n) {
    swap(m, n);
    ok = true;
  }
  int k1, k2, k;
  for (int i = 1; i <= T; i++) {
    scanf("%d%d%d", &k1, &k2, &k);
    if (ok) swap(k1, k2);
    if (k == -1)
      x[k1]++;
    else
      y[k1]++;
  }
  for (int i = 1; i <= n; i++)
    if (x[i] == 0 && y[i] == 0) {
      swap(x[i], x[n]);
      swap(y[i], y[n]);
      break;
    }
  scanf("%I64d", &p);
  f[0][0] = 1;
  for (int i = 0; i < m; i++)
    for (int j = 0; j <= i; j++) {
      f[i + 1][j] += f[i][j];
      f[i + 1][j] %= p;
      f[i + 1][j + 1] += f[i][j];
      f[i + 1][j + 1] %= p;
    }
  long long ans = 1;
  long long t;
  for (int i = 1; i < n; i++) {
    t = 0;
    for (int j = 0; j <= m - x[i] - y[i]; j++)
      if ((j + x[i]) % 2 == 1) {
        t = t + f[m - y[i] - x[i]][j];
        t %= p;
      }
    ans = ans * t;
    ans %= p;
  }
  if (n % 2 != m % 2)
    printf("0\n");
  else if (n == 1) {
    if (y[0] == 1)
      printf("0\n");
    else
      printf("1\n");
  } else
    printf("%I64d\n", ans);
  return 0;
}
