#include <bits/stdc++.h>
using namespace std;
long long n, m, i, j, k, sz, mod, dp[605][605], rd[605], xs[605][605], ans = 1;
vector<long long> ts, bi[605], nrd, ncd;
long long gcd(long long x, long long y) {
  if (y == 0) return x;
  return gcd(y, x % y);
}
long long lcm(long long x, long long y) { return x / gcd(x, y) * y; }
void dfs(int x) {
  int i;
  rd[x] = -1;
  ts.push_back(x);
  for (i = 0; i < bi[x].size(); i++) {
    rd[bi[x][i]]--;
    if (!rd[bi[x][i]]) {
      dfs(bi[x][i]);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n >> m >> mod;
  for (i = 1; i <= m; i++) {
    int x, y;
    cin >> x >> y;
    bi[x].push_back(y);
    rd[y]++;
  }
  for (i = 1; i <= n; i++) {
    if (rd[i] == 0) nrd.push_back(i);
    if (bi[i].size() == 0) ncd.push_back(i);
  }
  sz = nrd.size();
  for (i = 0; i < sz; i++) {
    dfs(nrd[i]);
    dp[nrd[i]][nrd[i]] = 1;
  }
  for (i = 0; i < ts.size(); i++) {
    for (j = 0; j < ts.size(); j++) {
      for (k = 0; k < bi[ts[j]].size(); k++) {
        dp[ts[i]][bi[ts[j]][k]] += dp[ts[i]][ts[j]];
        dp[ts[i]][bi[ts[j]][k]] %= mod;
      }
    }
  }
  for (i = 0; i < sz; i++) {
    for (j = 0; j < sz; j++) {
      xs[i][j] = dp[nrd[i]][ncd[j]];
    }
  }
  long long cnt = 0;
  for (i = 0; i < sz; i++) {
    for (j = i + 1; j < sz; j++) {
      while (xs[j][i]) {
        long long t = -xs[i][i] / xs[j][i];
        for (k = i; k < sz; k++) {
          xs[i][k] += t * xs[j][k];
          xs[i][k] %= mod;
          xs[i][k] += mod;
          xs[i][k] %= mod;
        }
        for (k = i; k < sz; k++) {
          swap(xs[i][k], xs[j][k]);
        }
        cnt++;
      }
    }
  }
  if (cnt & 1) ans = mod - 1;
  for (i = 0; i < sz; i++) {
    ans *= xs[i][i];
    ans %= mod;
  }
  cout << ans;
  return 0;
}
