#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int sum = 0, ff = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') ff = -1;
    ch = getchar();
  }
  while (isdigit(ch)) sum = sum * 10 + (ch ^ 48), ch = getchar();
  return sum * ff;
}
const int mo = 1e9 + 7;
long long n, m, k, p, cnt, fl;
long long ans;
int mp[1005][1005];
long long q_pow(long long x, long long y) {
  long long res = 1;
  while (y) {
    if (y & 1) res = res * x % p;
    y >>= 1;
    x = x * x % p;
  }
  return res;
}
int main() {
  n = read(), m = read(), k = read();
  cnt = (n - 1) * (m - 1) - k;
  for (int i = 1; i <= k; i++) {
    int x = read(), y = read(), z = read();
    if (n < m) swap(x, y);
    mp[x][y] = z;
  }
  if (n < m) swap(n, m);
  p = read();
  if (n % 2 != m % 2) {
    cout << 0;
    return 0;
  }
  for (int i = 1; i <= n; i++) {
    int cheak = 1;
    int num = 0;
    for (int j = 1; j <= m; j++) {
      if (mp[i][j]) num++, cheak *= mp[i][j];
    }
    if (num == m) {
      if (cheak != -1) {
        fl = 1;
        break;
      }
      cnt++;
    }
  }
  if (fl) {
    cout << 0;
    return 0;
  } else {
    cout << q_pow(2, cnt);
    return 0;
  }
}
