#include <bits/stdc++.h>
using namespace std;
long long MOD;
int X, Y;
int K, x[1010], y[1010], c[1010];
int cnt[1010];
long long two[1010];
int a[1010][1010];
bool func(void) {
  int i, j;
  for ((i) = 0; (i) < (int)(K); (i)++) a[x[i]][y[i]] = c[i];
  for ((i) = 0; (i) < (int)(X); (i)++) {
    int prod = 1;
    for ((j) = 0; (j) < (int)(Y); (j)++) prod *= a[i][j];
    if (prod == 1) return false;
  }
  for ((j) = 0; (j) < (int)(Y); (j)++) {
    int prod = 1;
    for ((i) = 0; (i) < (int)(X); (i)++) prod *= a[i][j];
    if (prod == 1) return false;
  }
  return true;
}
int main(void) {
  int i, j;
  scanf("%d%d%d", &X, &Y, &K);
  for ((i) = 0; (i) < (int)(K); (i)++) {
    scanf("%d%d%d", &x[i], &y[i], &c[i]);
    x[i]--;
    y[i]--;
  }
  cin >> MOD;
  if (X < Y) {
    swap(X, Y);
    for ((i) = 0; (i) < (int)(K); (i)++) swap(x[i], y[i]);
  }
  int empty = -1;
  for ((i) = 0; (i) < (int)(X); (i)++) {
    bool found = false;
    for ((j) = 0; (j) < (int)(K); (j)++)
      if (x[j] == i) found = true;
    if (!found) empty = i;
  }
  for ((i) = 0; (i) < (int)(X); (i)++) cnt[i] = Y;
  for ((i) = 0; (i) < (int)(K); (i)++) cnt[x[i]]--;
  two[0] = 1;
  for ((i) = 0; (i) < (int)(1009); (i)++) two[i + 1] = two[i] * 2 % MOD;
  long long ans = 1;
  for ((i) = 0; (i) < (int)(X); (i)++)
    if (i != empty && cnt[i] > 0) ans = ans * two[cnt[i] - 1] % MOD;
  if (X % 2 != Y % 2) ans = 0;
  if (!func()) ans = 0;
  cout << ans << endl;
  return 0;
}
