#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> a(k), b(k), c(k);
  for (int i = 0; i < k; ++i) {
    cin >> a[i] >> b[i] >> c[i];
    --a[i];
    --b[i];
  }
  int p;
  cin >> p;
  bool ok = true;
  if (n % 2 != m % 2) ok = false;
  int dane = k;
  for (int i = 0; i < n; ++i) {
    int ilo = 1, il = 0;
    for (int j = 0; j < k; ++j) {
      if (a[j] == i) {
        ++il;
        ilo *= c[j];
      }
    }
    if (il == m) {
      --n;
      dane -= m;
      if (ilo == 1) ok = false;
    }
  }
  for (int i = 0; i < m; ++i) {
    int ilo = 1, il = 0;
    for (int j = 0; j < k; ++j) {
      if (b[j] == i) {
        ++il;
        ilo *= c[j];
      }
    }
    if (il == n) {
      --m;
      dane -= n;
      if (ilo == 1) ok = false;
    }
  }
  int odp = 1;
  if (!ok)
    odp = 0;
  else {
    for (int i = 0; i < (n - 1) * (m - 1) - dane; ++i) odp = (odp * 2) % p;
  }
  cout << odp;
  return 0;
}
