#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, k;
  cin >> n >> m >> k;
  if ((n & 1) != (m & 1)) {
    cout << 0;
    return 0;
  }
  bool f = n > m;
  if (!f) swap(n, m);
  int a, c, full = 0;
  vector<int> num(n);
  vector<int> sign(n);
  for (int i = 0; i < k; ++i) {
    if (f)
      cin >> a >> c >> c;
    else
      cin >> a >> a >> c;
    --a;
    num[a]++;
    sign[a] ^= c == -1;
    if (num[a] == m)
      if (sign[a])
        ++full;
      else
        return (cout << 0), 0;
  }
  int p;
  cin >> p;
  int emp = m * n - m - k - (n - 1 - full);
  int res = 1;
  for (int i = 0; i < emp; ++i) res = (res << 1) % p;
  cout << res;
  return 0;
}
