#include <bits/stdc++.h>
using namespace std;
long long MOD = 1e9 + 7;
const long long SZ = 107;
const long long N = 6e2 + 7;
long long pwr(long long x, long long y) {
  long long r = 1LL;
  while (y) {
    if (y & 1) r = (r * x) % MOD;
    y >>= 1;
    x = (x * x) % MOD;
  }
  return r;
}
long long inv(long long x) { return pwr(x, MOD - 2ll); }
long long n, m, p;
long long d[N][N], cnt[N][N];
long long din[N], dout[N];
long long n0[N], n1[N];
bool vis[N];
long long source, c[N];
long long dfs(long long v) {
  if (v == source) return 1;
  if (vis[v]) {
    return c[v];
  }
  vis[v] = 1;
  long long ans = 0;
  for (long long i = 1; i <= n; i++) {
    if (d[i][v]) {
      ans += d[i][v] * dfs(i);
      ans %= p;
    }
  }
  return c[v] = ans;
}
long long det(vector<vector<long long>> &mat) {
  long long det = 1;
  long long n = mat.size();
  for (long long i = 0; i < n; i++) {
    long long sel = i;
    for (long long j = i + 1; j < n; j++) {
      if (abs(mat[j][i]) > abs(mat[sel][i])) sel = j;
    }
    if (mat[sel][i] == 0) {
      return 0;
    }
    if (sel != i) {
      swap(mat[i], mat[sel]);
      det *= -1;
    }
    det *= mat[i][i];
    det %= p;
    for (long long j = i + 1; j < n; j++) {
      mat[i][j] *= inv(mat[i][i]);
      mat[i][j] %= p;
    }
    for (long long j = 0; j < n; j++) {
      if (i == j) continue;
      for (long long k = i + 1; k < n; k++) {
        mat[j][k] -= mat[i][k] * mat[j][i];
        mat[j][k] %= p;
      }
    }
  }
  return det;
}
void solve() {
  cin >> n >> m >> p;
  MOD = p;
  for (long long i = 0; i < m; i++) {
    long long x, y;
    cin >> x >> y;
    d[x][y]++;
    dout[x]++;
    din[y]++;
  }
  long long kin = 0, kout = 0;
  for (long long i = 1; i <= n; i++) {
    if (din[i] == 0) n0[i] = ++kin;
    if (dout[i] == 0) n1[i] = ++kout;
  }
  for (long long i = 1; i <= n; i++) {
    if (!n0[i]) continue;
    memset(vis, 0, sizeof(vis));
    memset(c, 0, sizeof(c));
    source = i;
    for (long long j = 1; j <= n; j++) {
      if (!n1[j]) continue;
      cnt[n0[i]][n1[j]] = dfs(j);
    }
  }
  n = kin;
  vector<vector<long long>> mat(n, vector<long long>(n));
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= n; j++) {
      mat[i - 1][j - 1] = cnt[i][j];
    }
  }
  long long ans = det(mat);
  ans %= p;
  ans += p;
  ans %= p;
  cout << ans;
}
signed main() {
  long long t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
