#include <bits/stdc++.h>
using namespace std;
struct dato {
  int i, j, val;
};
int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<dato> input(k);
  for (int t = 0; t < k; t++) {
    dato &d = input[t];
    cin >> d.i >> d.j >> d.val;
    d.i--;
    d.j--;
  }
  long long int p;
  cin >> p;
  if (n > m) {
    for (int t = 0; t < k; t++) {
      dato &d = input[t];
      swap(d.i, d.j);
    }
    swap(n, m);
  }
  vector<int> numcol(m, 0);
  vector<int> prodcol(m, 1);
  for (int t = 0; t < k; t++) {
    dato &d = input[t];
    numcol[d.j]++;
    prodcol[d.j] *= d.val;
  }
  bool error = false;
  for (int j = 0; j < m and not error; j++)
    error = numcol[j] == n and prodcol[j] == 1;
  if (error or n % 2 != m % 2)
    cout << 0 << endl;
  else {
    int numllenas = 0;
    for (int j = 0; j < m; j++)
      if (numcol[j] == n) numllenas++;
    int e = (n - 1) * (m - 1) - k + numllenas;
    long long int r = 2;
    long long int a = 1;
    while (e > 0) {
      if (e % 2 == 0) {
        r = (r * r) % p;
        e /= 2;
      } else {
        a = (a * r) % p;
        e--;
      }
    }
    cout << a << endl;
  }
}
