#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
const long double eps = 1e-7;
const int inf = 1000000010;
const long long INF = 10000000000000010LL;
const int mod = 1000000007;
const int MAXN = 100010;
long long n, m, k, x, y, z, a, b, c, t1, t2, cnt, p;
long long X[MAXN];
long long Y[MAXN];
bool X2[MAXN];
bool Y2[MAXN];
long long powmod(long long a, long long b) {
  if (!b) return 1;
  if (b & 1) return a * powmod(a, b - 1) % p;
  return powmod(a * a % p, b >> 1);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m >> k;
  if ((m ^ n) & 1) return cout << 0 << '\n', 0;
  ;
  for (int i = 0; i < k; i++) {
    cin >> a >> b >> c;
    X[a]++;
    Y[b]++;
    if (c == -1) {
      X2[a] ^= 1;
      Y2[b] ^= 1;
    }
  }
  for (int i = 1; i <= n; i++)
    if (X[i] == m) {
      if (!X2[i]) return cout << 0 << '\n', 0;
      ;
      t1++;
    }
  for (int i = 1; i <= m; i++)
    if (Y[i] == n) {
      if (!Y2[i]) return cout << 0 << '\n', 0;
      ;
      t2++;
    }
  cnt = (n - t1 - 1) * (m - t2 - 1) - (k - t1 * m - t2 * n + t1 * t2);
  cin >> p;
  cout << powmod(2, cnt) << endl;
  return 0;
}
