#include <bits/stdc++.h>
using namespace std;
long long n, m, temp, x, y, z;
long long mod, ans = 1, idn, idm;
long long sum[1200], cnt[1200];
long long mapp[1200][1200];
inline long long ksm(long long k) {
  if (k < 0) return 1;
  long long tmp = 1;
  long long base = 2;
  while (k) {
    if (k & 1) tmp = (tmp * base) % mod;
    base = (base * base) % mod;
    k >>= 1;
  }
  return tmp;
}
signed main() {
  scanf("%lld%lld%lld", &n, &m, &temp);
  if (n < m) {
    swap(n, m);
    for (register long long i = 1; i <= temp; ++i) {
      scanf("%lld%lld%lld", &x, &y, &z);
      mapp[y][x] = z;
    }
  } else {
    for (register long long i = 1; i <= temp; ++i) {
      scanf("%lld%lld%lld", &x, &y, &z);
      mapp[x][y] = z;
    }
  }
  scanf("%lld", &mod);
  for (register long long i = 1; i <= n; ++i) {
    sum[i] = 1;
    for (register long long j = 1; j <= m; ++j)
      if (mapp[i][j] != 0) {
        cnt[i]++;
        sum[i] *= mapp[i][j];
      }
  }
  if ((n & 1) != (m & 1)) {
    puts("0");
    return 0;
  }
  bool flag = 0;
  for (register long long i = 1; i <= n; ++i) {
    if (cnt[i] == m && sum[i] == 1) {
      puts("0");
      return 0;
    }
    if (cnt[i]) {
      ans *= ksm(m - cnt[i] - 1);
      ans %= mod;
    } else if (!cnt[i] && !flag) {
      flag = 1;
      continue;
    } else if (!cnt[i] && flag == 1) {
      ans *= ksm(m - 1);
      ans %= mod;
    }
  }
  printf("%lld", ans);
}
