#include <bits/stdc++.h>
using namespace std;
const int maxn = 1010;
int n, m, size1[maxn], size2[maxn], k;
long long c[maxn][maxn], ans, mo;
int main() {
  scanf("%d%d", &n, &m);
  if (n % 2 != m % 2) {
    printf("0\n");
    return 0;
  }
  bool s = false;
  if (n < m) {
    swap(n, m);
    s = true;
  }
  scanf("%d", &k);
  for (int a = 1; a <= k; a++) {
    int x, y, v;
    scanf("%d%d%d", &x, &y, &v);
    if (s) swap(x, y);
    size1[x]++;
    if (v == -1) size2[x]++;
  }
  scanf("%I64d", &mo);
  c[0][0] = 1;
  c[1][1] = 1;
  c[1][0] = 1;
  for (int a = 2; a <= 1001; a++) {
    c[a][0] = 1;
    for (int b = 1; b <= a; b++) c[a][b] = (c[a - 1][b - 1] + c[a - 1][b]) % mo;
  }
  for (int a = 1; a < n; a++)
    if (size1[a] == 0) swap(size1[a], size1[n]), swap(size2[a], size2[n]);
  ans = 1;
  for (int a = 1; a < n; a++) {
    long long nowans = 0;
    if (size2[a] % 2 == 0) {
      for (int b = 1; b <= m - size1[a]; b += 2)
        nowans = (nowans + c[m - size1[a]][b]) % mo;
    } else {
      for (int b = 0; b <= m - size1[a]; b += 2)
        nowans = (nowans + c[m - size1[a]][b]) % mo;
    }
    ans = ans * nowans % mo;
  }
  printf("%I64d\n", ans % mo);
  return 0;
}
