#include <bits/stdc++.h>
using namespace std;
int t[1010][1010];
int main() {
  cout << fixed;
  cout.precision(9);
  long long n, m, k, p;
  cin >> n >> m >> k;
  if (n % 2 != m % 2) {
    cout << "0\n" << endl;
    return 0;
  }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      t[i][j] = 0;
    }
  for (int i = 0; i < k; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    t[a - 1][b - 1] = c;
  }
  cin >> p;
  int tmp = 0;
  for (int i = 0; i < n; i++) {
    int c = 1;
    for (int j = 0; j < m; j++) c *= t[i][j];
    if (c == 1) {
      cout << "0\n" << endl;
      return 0;
    }
    if (c != 0) {
      ++tmp;
    }
  }
  for (int i = 0; i < m; i++) {
    int c = 1;
    for (int j = 0; j < n; j++) c *= t[j][i];
    if (c == 1) {
      cout << "0\n" << endl;
      return 0;
    }
    if (c != 0) {
      ++tmp;
    }
  }
  long long r = m * n - k - (n + m - 1 - tmp);
  long long res = 1;
  while (r--) {
    res = 2 * res % p;
  }
  cout << res << endl;
  return 0;
}
