#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int inf = 0x3f3f3f3f;
const int maxn = 1005;
int n, m, k, p;
int p2[maxn];
void init() {
  p2[0] = 1;
  for (int i = 1; i <= maxn - 1; i++) p2[i] = (p2[i - 1] << 1) % p;
}
int row[maxn];
int mul[maxn];
int main() {
  cin >> n >> m >> k;
  bool changed = 0;
  if (n < m) {
    swap(n, m);
    changed = 1;
  }
  for (int i = 1; i <= n; i++) mul[i] = 1;
  for (int i = 1; i <= k; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    if (changed) swap(x, y);
    row[x]++;
    mul[x] *= z;
  }
  cin >> p;
  init();
  if ((n + m) & 1) {
    cout << 0 << endl;
    return 0;
  }
  int s = 1;
  while (row[s] != 0) s++;
  ll ans = 1;
  for (int i = 1; i <= n; i++) {
    if (i == s) continue;
    if (row[i] == m && mul[i] == 1) {
      ans = 0;
      break;
    }
    if (m - row[i] >= 2) (ans *= (p2[m - row[i] - 1])) %= p;
  }
  cout << ans << "\n";
  return 0;
}
