#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1000 + 10;
int n[2];
int pos[2][MAX_N];
int adj[MAX_N][MAX_N];
int dep[2][MAX_N];
bool mark[2][MAX_N];
long long ans = 1, p;
int dfs(int i, int j, int d) {
  mark[i][j] = true;
  dep[i][j] = d;
  int ret = 1;
  for (int k = 0; k < (n[!i]); k++) {
    if ((i && adj[k][j] || !i && adj[j][k])) {
      ret *= i ? adj[k][j] : adj[j][k];
      continue;
    }
    if (mark[!i][k]) {
      if (dep[!i][k] != d - 1) {
        if (i)
          adj[k][j] = 1;
        else
          adj[j][k] = 1;
        ans = (2 * ans) % p;
      }
      continue;
    }
    int res = dfs(!i, k, d + 1);
    if (i)
      adj[k][j] = res;
    else
      adj[j][k] = res;
    ret *= res;
  }
  return ret * -1;
}
int main() {
  scanf("%d %d", &n[0], &n[1]);
  int k;
  scanf("%d", &k);
  for (int i = 0; i < (2); i++)
    for (int j = 0; j < (n[i]); j++) pos[i][j] = 1;
  for (int i = 0; i < (k); i++) {
    int a, b;
    scanf("%d %d", &a, &b);
    a--;
    b--;
    scanf("%d", &adj[a][b]);
  }
  cin >> p;
  for (int i = 0; i < (2); i++)
    for (int j = 0; j < (n[i]); j++)
      if (!mark[i][j])
        if (dfs(i, j, 0) == -1) {
          printf("0\n");
          return 0;
        }
  cout << ans << endl;
  return 0;
}
