#include <bits/stdc++.h>
int n, m, k;
int g[2000][2000];
long long mod;
void read_input() {
  memset(g, 0, sizeof(g));
  scanf("%d %d", &n, &m);
  scanf("%d", &k);
  for (int i = 0; i < k; i++) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    if (n >= m) {
      g[a][b] = c;
    } else {
      g[b][a] = c;
    }
  }
  if (n < m) {
    n ^= m;
    m ^= n;
    n ^= m;
  }
  scanf("%I64d", &mod);
}
long long get_ans() {
  if ((n + m) % 2 == 1) return 0;
  bool found_empty = false;
  long long prod = 1;
  for (int i = 1; i <= n; i++) {
    int num_zero = 0;
    int cprod = 1;
    for (int j = 1; j <= m; j++) {
      cprod *= g[i][j];
      if (g[i][j] == 0) {
        num_zero++;
      }
    }
    if (cprod == 1) return 0;
    if (num_zero == m && !found_empty) {
      found_empty = true;
      continue;
    }
    for (int j = 1; j < num_zero; j++) {
      prod *= 2;
      prod %= mod;
    }
  }
  return prod;
}
int main() {
  read_input();
  printf("%I64d\n", get_ans());
}
