#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
int n, m, k, p, pow2[N], cnt[N], mul[N], ans = 1;
bool is_change;
int main() {
  scanf("%d%d%d", &n, &m, &k);
  if (n < m) swap(n, m), is_change = true;
  for (int i = 1; i <= n; i++) mul[i] = 1;
  for (int i = 1, x, y, v; i <= k; i++) {
    scanf("%d%d%d", &x, &y, &v);
    if (is_change) swap(x, y);
    cnt[x]++, mul[x] *= v;
  }
  scanf("%d", &p);
  if ((n & 1) != (m & 1)) {
    puts("0");
    return 0;
  }
  pow2[0] = 1;
  for (int i = 1; i <= n; i++) pow2[i] = (pow2[i - 1] * 2) % p;
  for (int i = 1; i < n; i++)
    if (!cnt[i]) {
      swap(cnt[i], cnt[n]);
      swap(mul[i], mul[n]);
      break;
    }
  for (int i = 1; i < n && ans; i++) {
    if (cnt[i] == m) {
      if (mul[i] == 1) ans = 0;
    } else
      ans = ans * 1ll * pow2[m - cnt[i] - 1] % p;
  }
  printf("%d\n", ans);
  return 0;
}
