#include <bits/stdc++.h>
using namespace std;
int n, m, k, r[1011], c[1011], P, vr[1011], vc[1011], t, ans(1);
int main() {
  cin >> n >> m >> k;
  if ((n + m) & 1) goto end;
  t = (n - 1) * (m - 1) - k;
  for (int i = 0, x, y, z; i < k; ++i) {
    cin >> x >> y >> z;
    vr[x] ^= z == -1, vc[y] ^= z == -1;
    if (++r[x] == m) {
      if (vr[x])
        ++t;
      else
        goto end;
    }
    if (++c[y] == n) {
      if (vc[y])
        ++t;
      else
        goto end;
    }
  }
  cin >> P;
  for (int i = 0; i < t; ++i) (ans <<= 1) %= P;
  return cout << ans << endl, 0;
end:
  return cout << "0\n", 0;
}
