#include <bits/stdc++.h>
using namespace std;
const int N = 1111;
int n, m, num[N], all[N];
long long p, C[N][N];
int main() {
  cin >> n >> m;
  if (n % 2 != m % 2) {
    printf("0\n");
    return 0;
  }
  bool SWAP = 0;
  if (n < m) {
    SWAP = 1;
    swap(n, m);
  }
  int k;
  cin >> k;
  for (int i = (1); i <= (n); i++) all[i] = m;
  for (int i = (1); i <= (k); i++) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    if (SWAP) swap(a, b);
    if (c == -1)
      num[a]++;
    else
      all[a]--;
  }
  cin >> p;
  for (int i = (0); i <= (1000); i++) {
    C[i][0] = C[i][i] = 1;
    for (int j = (1); j <= (i - 1); j++)
      C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % p;
  }
  bool SKIP = 0;
  long long ans = 1;
  for (int i = (1); i <= (n); i++) {
    if (num[i] == 0 && all[i] == m && !SKIP) {
      SKIP = 1;
      continue;
    }
    long long now = 0;
    for (int j = num[i]; j <= all[i]; j++) {
      if (j % 2) {
        (now += C[all[i] - num[i]][j - num[i]]) %= p;
      }
    }
    (ans *= now) %= p;
  }
  cout << ans << endl;
  return 0;
}
