#include <bits/stdc++.h>
using namespace std;
int n, m;
int a[1010][1010];
long long ans = 0, mod;
long long c[1010][1010];
void work() {
  int i, j;
  c[0][0] = 1;
  for (i = 1; i <= m; i++) {
    c[i][0] = 1;
    for (j = 1; j <= i; j++) c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % mod;
  }
}
int main() {
  int i, j, flag, flag2, k, s, x, y;
  long long s1 = 0;
  scanf("%d%d", &n, &m);
  if (n > m) {
    scanf("%d", &k);
    for (i = 1; i <= k; i++) {
      scanf("%d%d", &x, &y);
      scanf("%d", &a[x][y]);
    }
  } else {
    swap(n, m);
    scanf("%d", &k);
    for (i = 1; i <= k; i++) {
      scanf("%d%d", &x, &y);
      scanf("%d", &a[y][x]);
    }
  }
  scanf("%d", &mod);
  work();
  if ((n + m) % 2 == 1) {
    printf("0\n");
    return 0;
  }
  for (i = 1; i <= n; i++) {
    s = 1;
    for (j = 1; j <= m; j++) s *= a[i][j];
    if (s == 1) {
      printf("0\n");
      return 0;
    }
  }
  for (j = 1; j <= m; j++) {
    s = 1;
    for (i = 1; i <= n; i++) s *= a[i][j];
    if (s == 1) {
      printf("0\n");
      return 0;
    }
  }
  ans = 1;
  flag = 0;
  flag2 = 0;
  for (i = 1; i <= n; i++) {
    flag2 = 0;
    if (flag == 0) {
      flag = 1;
      for (j = 1; j <= m; j++)
        if (a[i][j] != 0) {
          flag = 0;
          break;
        }
      flag2 = 1;
    }
    if (flag == 0 || (flag == 1 && flag2 == 0)) {
      s = 1;
      k = 0;
      for (j = 1; j <= m; j++)
        if (a[i][j] != 0) {
          s *= a[i][j];
          k++;
        }
      s1 = 0;
      for (j = 0; j <= m - k; j++)
        if ((j % 2 == 0 && s == -1) || (j % 2 == 1 && s == 1))
          s1 = (s1 + c[m - k][j]) % mod;
      ans = ans * s1 % mod;
    }
  }
  cout << ans << endl;
  return 0;
}
