#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int x = 0;
  char ch = getchar();
  bool positive = 1;
  for (; ch < '0' || ch > '9'; ch = getchar())
    if (ch == '-') positive = 0;
  for (; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
  return positive ? x : -x;
}
int n, m, k, x, y, z, MOD, cnt, ans = 1, c[1005], d[1005], e[1005], f[1005],
                                p[1005];
int work() {
  n = read(), m = read(), k = read();
  if ((n + m) & 1) return 0;
  for (int i = 1; i <= n || i <= m; i++) e[i] = f[i] = 1;
  for (int i = 1; i <= k; i++) {
    x = read(), y = read(), z = read();
    c[x]++, d[y]++;
    e[x] *= z, f[y] *= z;
  }
  scanf("%d", &MOD), cnt = n * m - k - n - m + 1;
  for (int i = 1; i <= n; i++)
    if (c[i] == m)
      if (e[i] == 1)
        return 0;
      else
        cnt++;
  for (int j = 1; j <= m; j++)
    if (d[j] == n)
      if (f[j] == 1)
        return 0;
      else
        cnt++;
  for (int i = 1; i <= cnt; i++) ans = ans * 2 % MOD;
  return ans;
}
int main() {
  printf("%d\n", work());
  return 0;
}
