#include <bits/stdc++.h>
using namespace std;
const int iinf = 1e9 + 7;
const long long linf = 1ll << 60;
const double dinf = 1e10;
void scf(int &x) {
  bool f = 0;
  x = 0;
  char c = getchar();
  while ((c < '0' || c > '9') && c != '-') c = getchar();
  if (c == '-') {
    f = 1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  if (f) x = -x;
  return;
}
void scf(int &x, int &y) {
  scf(x);
  return scf(y);
}
void scf(int &x, int &y, int &z) {
  scf(x);
  scf(y);
  return scf(z);
}
const int N = 1010;
bool f;
int n, m, k, r;
int num[N][N];
long long M, ans = 1ll;
long long c[N][N];
int main() {
  scf(n, m, k);
  if (n < m) n ^= m, m ^= n, n ^= m, f = 1;
  if (n + m & 1) {
    puts("0");
    return 0;
  }
  while (k--) {
    int i, j, x;
    scf(i, j, x);
    if (f) i ^= j, j ^= i, i ^= j;
    num[i][j] = x;
  }
  int p;
  scf(p);
  M = (long long)p;
  for (int i = 0; i <= n; i++) {
    c[i][0] = c[i][i] = 1ll;
    for (int j = 1; j <= i - 1; j++) {
      c[i][j] = c[i - 1][j - 1] + c[i - 1][j];
      (c[i][j] >= M) && (c[i][j] -= M);
    }
  }
  for (int i = 1; i <= n; i++) {
    f = 1;
    int cur = 1, cnt = m;
    for (int j = 1; j <= m; j++)
      if (num[i][j] != 0) f = 0, cnt--, cur *= num[i][j];
    if (f && !r) {
      r = i;
      continue;
    }
    if (cur == 1 && !cnt) {
      puts("0");
      return 0;
    }
    long long res = 0;
    for (int j = (cur == 1); j <= cnt; j += 2) {
      res += c[cnt][j];
      (res >= M) && (res -= M);
    }
    ans = (ans * res) % M;
  }
  printf("%I64d\n", ans);
  return 0;
}
