#include <bits/stdc++.h>
using namespace std;
int gi() {
  int w = 0;
  bool q = 1;
  char c = getchar();
  while ((c < '0' || c > '9') && c != '-') c = getchar();
  if (c == '-') q = 0, c = getchar();
  while (c >= '0' && c <= '9') w = w * 10 + c - '0', c = getchar();
  return q ? w : -w;
}
const int N = 1010;
int h[N];
int ful[N], p[N];
int main() {
  int n = gi(), m = gi(), x, y, ans = 1, i, T = gi(), mod;
  bool flag = true;
  if ((n ^ m) & 1) return puts("0"), 0;
  while (T--) {
    x = gi(), y = gi();
    n < m ? swap(x, y) : (void)0;
    ful[x]++;
    if (gi() == -1) h[x] ^= 1;
  }
  n < m ? swap(n, m) : (void)0;
  mod = gi();
  for (i = p[0] = 1; i <= m; i++) p[i] = (p[i - 1] << 1) % mod;
  for (i = 1; i <= n; i++)
    if (!ful[i] && flag)
      flag = false;
    else if (ful[i] < m)
      ans = 1LL * ans * p[m - ful[i] - 1] % mod;
    else if (!h[i])
      return puts("0"), 0;
  printf("%d\n", ans);
  return 0;
}
