#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f, maxn = 1005;
int mod_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 n, m, k, p, f[maxn][maxn];
int main() {
  while (~scanf("%d%d", &n, &m)) {
    memset(f, 0, sizeof(f));
    scanf("%d", &k);
    for (int i = 1; i <= k; i++) {
      int a, b, c;
      scanf("%d%d%d", &a, &b, &c);
      if (n < m) swap(a, b);
      f[a][b] = c;
    }
    if (n < m) swap(n, m);
    scanf("%d", &p);
    if ((n - m) & 1) {
      printf("0\n");
      continue;
    }
    int cnt = (n - 1) * (m - 1) - k, flag = 0;
    for (int i = 1; i <= n; i++) {
      int num = 0, temp = 1;
      for (int j = 1; j <= m; j++)
        if (f[i][j] != 0) num++, temp *= f[i][j];
      if (num == m) {
        if (temp != -1) {
          flag = 1;
          break;
        }
        cnt++;
      }
    }
    if (flag)
      printf("0\n");
    else
      printf("%d\n", mod_pow(2, cnt, p));
  }
  return 0;
}
