#include <bits/stdc++.h>
using namespace std;
int n, m, f, p, a[1024][1024];
set<int> row[1024], col[1024];
int r[1024], c[1024];
set<pair<int, int> > w;
int main() {
  cin >> n >> m >> f;
  fill(r, r + 1024, 1);
  fill(c, c + 1024, 1);
  for (int i(0); i < f; ++i) {
    int x, y, z;
    cin >> x >> y >> z;
    a[--x][--y] = z;
    r[x] *= z;
    c[y] *= z;
  }
  cin >> p;
  int res = 1;
  for (int i(0); i < n; ++i)
    for (int j(0); j < m; ++j)
      if (!a[i][j]) {
        row[i].insert(j);
        col[j].insert(i);
        w.insert(pair<int, int>(i, j));
      }
  queue<int> Qr, Qc;
  for (int i(0); i < n; ++i)
    if (int((row[i]).size()) == 1) Qr.push(i);
  for (int j(0); j < m; ++j)
    if (int((col[j]).size()) == 1) Qc.push(j);
  for (;;) {
    bool ok = true;
    for (; int((Qr).size()) || int((Qc).size());) {
      ok = false;
      if (int((Qr).size())) {
        int i = Qr.front();
        Qr.pop();
        if (row[i].size() == 0) continue;
        int j = *row[i].begin();
        row[i].clear();
        a[i][j] = -r[i];
        w.erase(pair<int, int>(i, j));
        r[i] *= a[i][j];
        c[j] *= a[i][j];
        col[j].erase(i);
        if (int((col[j]).size()) == 1) Qc.push(j);
      } else {
        int j = Qc.front();
        Qc.pop();
        if (col[j].size() == 0) continue;
        int i = *col[j].begin();
        col[j].clear();
        a[i][j] = -c[j];
        w.erase(pair<int, int>(i, j));
        r[i] *= a[i][j];
        c[j] *= a[i][j];
        row[i].erase(j);
        if (int((row[i]).size()) == 1) Qr.push(i);
      }
    }
    if (int((w).size())) {
      ok = false;
      pair<int, int> t = *w.begin();
      a[t.first][t.second] = 1;
      w.erase(w.begin());
      row[t.first].erase(t.second);
      col[t.second].erase(t.first);
      res = (res + res) % p;
      if (int((row[t.first]).size()) == 1) Qr.push(t.first);
      if (int((col[t.second]).size()) == 1) Qc.push(t.second);
    }
    if (ok) break;
  }
  for (int i(0); i < n; ++i)
    if (r[i] == 1) res = 0;
  for (int j(0); j < m; ++j)
    if (c[j] == 1) res = 0;
  cout << res << endl;
  return 0;
}
