#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1000 + 10;
bool markc[MAX_N], markr[MAX_N];
int tc[MAX_N], tr[MAX_N];
long long ans = 1, p;
long long f(int t) {
  long long ff = 1;
  for (int i = 1; i < t; i++) ff = (ff * (long long)2) % p;
  return ff;
}
int main() {
  int n, m, t;
  cin >> n >> m >> t;
  for (int i = 0; i < n; i++) tr[i] = m;
  for (int i = 0; i < m; i++) tc[i] = n;
  for (int i = 0; i < t; i++) {
    int x, y, wa;
    cin >> x >> y >> wa;
    x--;
    y--;
    if (wa == -1) {
      markr[x] = !markr[x];
      markc[y] = !markc[y];
    }
    tr[x]--;
    tc[y]--;
  }
  cin >> p;
  if (n % 2 != m % 2)
    ans = 0;
  else {
    int temp;
    if (n < m) {
      for (int i = 0; i < m; i++)
        if (tc[i] == n) {
          temp = i;
          break;
        }
      for (int i = 0; i < m; i++)
        if (i != temp) ans = (ans * f(tc[i])) % p;
      for (int i = 0; i < m; i++)
        if (tc[i] == 0 && !markc[i]) ans = 0;
    } else {
      for (int i = 0; i < n; i++)
        if (tr[i] == m) {
          temp = i;
          break;
        }
      for (int i = 0; i < n; i++)
        if (i != temp) ans = (ans * f(tr[i])) % p;
      for (int i = 0; i < n; i++)
        if (tr[i] == 0 && !markr[i]) ans = 0;
    }
  }
  cout << ans << endl;
  return 0;
}
