#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long f = 1, x = 0;
  char ch;
  do {
    ch = getchar();
    if (ch == '-') f = -1;
  } while (ch < '0' || ch > '9');
  do {
    x = (x << 3) + (x << 1) + ch - '0';
    ch = getchar();
  } while (ch >= '0' && ch <= '9');
  return f * x;
}
const int MAXN = 100000 + 10;
long long n, m, k, p;
long long cnt[MAXN], mul[MAXN];
inline long long Pow(long long a, long long b) {
  long long res = 1, mul = a;
  while (b) {
    if (b & 1) res = (res * mul) % p;
    mul = (mul * mul) % p;
    b >>= 1;
  }
  return res;
}
int main() {
  n = read(), m = read();
  k = read();
  bool flag = 1;
  if (n < m) {
    swap(n, m);
    flag = 0;
  }
  if (n % 2 + m % 2 == 1) {
    cout << 0 << endl;
    return 0;
  }
  for (int i = 1; i <= n; i++) mul[i] = 1;
  int tot = 0;
  for (int i = 1; i <= k; i++) {
    int a = read(), b = read(), c = read();
    if (!flag) swap(a, b);
    cnt[a]++;
    mul[a] *= c;
  }
  for (int i = 1; i <= n; i++) {
    if (cnt[i] == m && mul[i] == 1) {
      cout << 0 << endl;
      return 0;
    }
    tot += min(m - 1, cnt[i]);
  }
  p = read();
  cout << Pow(2, (long long)(n - 1) * (m - 1) - tot) << endl;
}
