#include <bits/stdc++.h>
using namespace std;
int n, m, k, mod;
int mat[1005][1005];
int main() {
  cin >> n >> m >> k;
  for (int i = 1; i <= k; i++) {
    int x, y;
    cin >> x >> y;
    if (n > m) swap(x, y);
    cin >> mat[x][y];
  }
  if (n > m) swap(n, m);
  if (n % 2 != m % 2) {
    cout << 0 << endl;
    return 0;
  }
  int free = 0;
  for (int j = 1; j <= m; j++) {
    int tmp = 0, mult = 1;
    for (int i = 1; i <= n; i++)
      if (mat[i][j] == 0)
        tmp++;
      else
        mult *= mat[i][j];
    if (tmp == 0 && mult != -1) {
      cout << 0 << endl;
      return 0;
    }
    free += max(0, tmp - 1);
  }
  cin >> mod;
  free -= (n - 1);
  int res = 1;
  while (free) {
    res = res * 2;
    if (res >= mod) res -= mod;
    free--;
  }
  cout << res << endl;
  return 0;
}
