#include <bits/stdc++.h>
using namespace std;
int n, m, k, p, a, b, c, f[1005][1005];
inline int pow(int a, int b, int c) {
  int ans = 1;
  while (b) {
    if (b & 1) ans = (long long)ans * a % c;
    a = (long long)a * a % c;
    b >>= 1;
  }
  return ans;
}
int main() {
  int x, y, z;
  cin >> n >> m >> k;
  for (int i = 1; i <= k; i++) {
    cin >> x >> y >> z;
    if (n < m) swap(x, y);
    f[x][y] = z;
  }
  if (n < m) swap(n, m);
  cin >> p;
  if ((n - m) & 1) {
    puts("0");
    return 0;
  }
  int cnt = (n - 1) * (m - 1) - k, flag = 0;
  for (int i = 1; i <= n; i++) {
    int num = 0, tp = 1;
    for (int j = 1; j <= m; j++)
      if (f[i][j]) num++, tp *= f[i][j];
    if (num == m) {
      if (tp != -1) {
        flag = 1;
        break;
      }
      cnt++;
    }
  }
  if (flag)
    puts("0");
  else
    printf("%d\n", pow(2, cnt, p));
  return 0;
}
