#include <bits/stdc++.h>
using namespace std;
int n, m, k, p, x[1111], y[1111], z[1111], cur, cnt, ans, pw[1111];
bool f, fl;
vector<int> v[1111];
int main() {
  scanf("%d%d", &n, &m);
  scanf("%d", &k);
  if ((n + m) & 1) {
    printf("0\n");
    return 0;
  }
  if (n < m) {
    swap(n, m);
    f = 1;
  }
  for (int i = 1; i <= k; i++) {
    scanf("%d%d%d", &x[i], &y[i], &z[i]);
    if (f) swap(x[i], y[i]);
    v[x[i]].push_back(i);
  }
  scanf("%d", &p);
  pw[0] = 1;
  for (int i = 1; i <= n; i++) pw[i] = pw[i - 1] * 2 % p;
  ans = 1;
  for (int i = 1; i <= n; i++) {
    cnt = 0;
    cur = 1;
    for (int j = 0; j < v[i].size(); j++) {
      cnt++;
      cur *= z[v[i][j]];
    }
    if (!cnt) {
      if (!fl) {
        fl = 1;
        continue;
      }
      ans = 1ll * ans * pw[m - 1] % p;
    } else {
      if (cnt == m) {
        if (cur == 1) ans = 0;
      } else {
        ans = 1ll * ans * pw[m - cnt - 1] % p;
      }
    }
  }
  printf("%d\n", ans);
  return 0;
}
