#include <bits/stdc++.h>
int a[1001], b[1001], c[1001], cnt[1001], yh[1001], n, m, num, mod, tot;
long long mi[1001], ans;
int main() {
  scanf("%d%d", &n, &m);
  if ((n ^ m) & 1) {
    puts("0");
    return 0;
  }
  scanf("%d", &num);
  for (int i = 1; i <= num; i++) {
    scanf("%d%d%d", &a[i], &b[i], &c[i]),
        (c[i] == -1) ? (c[i] = 1) : (c[i] = 0);
    if (n < m) std::swap(a[i], b[i]);
  }
  scanf("%d", &mod);
  mi[0] = 1;
  for (int i = 1; i <= m; i++) mi[i] = (mi[i - 1] * 2ll) % mod;
  if (n < m) std::swap(n, m);
  for (int i = 1; i <= num; i++) cnt[a[i]]++, yh[a[i]] ^= c[i];
  for (int i = 1; i <= n; i++)
    if ((cnt[i] == m) && (yh[i] == 0)) return 0 * puts("0");
  tot = 0, ans = 1;
  for (int i = 1; i <= n; i++) {
    if (cnt[i] == 0) {
      tot++;
      if (tot > 1) ans = (ans * mi[m - 1]) % mod;
    } else if (cnt[i] == m)
      continue;
    else
      ans = (ans * mi[m - cnt[i] - 1]) % mod;
  }
  printf("%lld", ans);
  return 0;
}
