#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
int n, m, K, a[N][N], P, power[N];
int main() {
  cin >> n >> m >> K;
  int x, y, z;
  while (K--) {
    scanf("%d%d%d", &x, &y, &z);
    if (n > m)
      a[x][y] = z;
    else
      a[y][x] = z;
  }
  if (n < m) swap(n, m);
  cin >> P;
  power[0] = 1;
  for (int i = (1); i <= (m); i++) power[i] = power[i - 1] * 2 % P;
  int t = 0;
  long long ans = 1;
  for (int i = (1); i <= (n); i++) {
    bool flag = 0;
    for (int j = (1); j <= (m); j++)
      if (a[i][j]) flag = 1;
    if (!flag) {
      t = i;
      break;
    }
  }
  for (int i = (1); i <= (n); i++)
    if (i != t) {
      int cnt = 0, mul = 1;
      for (int j = (1); j <= (m); j++) {
        if (a[i][j] == 0)
          cnt++;
        else
          mul = mul * a[i][j];
      }
      if (cnt)
        ans = ans * power[cnt - 1] % P;
      else if (mul == 1)
        ans = 0;
    }
  if ((n % 2) ^ (m % 2))
    ans = 0;
  else if (m == 1) {
    if (n % 2 == 0) ans = 0;
    for (int i = (1); i <= (n); i++)
      if (a[i][1] == 1) ans = 0;
  }
  cout << ans << endl;
  return 0;
}
