#include <bits/stdc++.h>
using namespace std;
const int N = 600 + 5, M = 1e6 + 5;
int n, m, mo, x, y, ru[N], chu[N], d[N], e[N], dl[N], q1, q2, a[N][N], D, i, j,
    f[N][N], p, l, k;
int tt, fst[N], nxt[M], b[M];
void cr(int x, int y) {
  tt++;
  b[tt] = y;
  nxt[tt] = fst[x];
  fst[x] = tt;
}
int ksm(int x, int y) {
  int ret = 1;
  while (y) {
    if (y & 1) ret = 1ll * ret * x % mo;
    y >>= 1;
    x = 1ll * x * x % mo;
  }
  return ret;
}
void dp() {
  while (q1 < q2)
    for (p = fst[dl[++q1]]; p; p = nxt[p])
      if (!(--ru[b[p]])) dl[++q2] = b[p];
  for (l = 1; l <= d[0]; l++) {
    x = d[l];
    f[x][x] = 1;
    for (i = 1; i <= n; i++)
      for (p = fst[(y = dl[i])]; p; p = nxt[p]) (f[x][b[p]] += f[x][y]) %= mo;
  }
}
int det(int K) {
  int sig = 1, ans = 1;
  for (int i, j, k = 1, Inv, t; k <= K; k++) {
    for (i = k; i <= K && !a[i][k]; i++)
      ;
    if (i > K) return 0;
    if (i > k) {
      sig = -sig;
      for (j = k; j <= K; j++) swap(a[k][j], a[i][j]);
    }
    Inv = mo - ksm(a[k][k], mo - 2);
    for (i = k + 1; i <= K; i++) {
      t = (long long)Inv * a[i][k] % mo;
      for (j = k; j <= K; j++)
        a[i][j] = (a[i][j] + (long long)t * a[k][j]) % mo;
    }
  }
  for (int i = 1; i <= K; i++) ans = (long long)ans * a[i][i] % mo;
  if (sig == -1) ans = -ans;
  if (ans < 0) ans += mo;
  return ans;
}
int main() {
  scanf("%d %d %d", &n, &m, &mo);
  for (i = 1; i <= m; i++) {
    scanf("%d %d", &x, &y);
    cr(x, y);
    ru[y]++;
    chu[x]++;
  }
  for (i = 1; i <= n; i++) {
    if (!ru[i]) d[++d[0]] = i, dl[++q2] = i;
    if (!chu[i]) e[++e[0]] = i;
  }
  dp();
  for (i = 1; i <= d[0]; i++)
    for (j = 1; j <= d[0]; j++) {
      a[i][j] = f[d[i]][e[j]];
    }
  D = det(d[0]);
  printf("%d", (D + mo) % mo);
}
