#include <bits/stdc++.h>
unsigned P;
inline unsigned long long moc(unsigned long long a) {
  return a < P ? a : a - P;
}
inline unsigned long long mod(unsigned long long a) {
  return a < P ? a : a % P;
}
inline unsigned long long inv(unsigned long long a) {
  unsigned long long v = 1;
  while (a > 1) v = mod(v * (P - P / a)), a = P % a;
  return v;
}
struct edg {
  int v;
  edg* e;
} ES[100000], *ER = ES, *EA[600 + 1];
inline void edg_ins(int u, int v) { *ER = (edg){v, EA[u]}, EA[u] = ER++; }
int V, E, N, i, k, u, v, d[600 + 1];
bool vis[600 + 1];
unsigned long long f[600 + 1][600], a[600][600];
inline void dfs(int u) {
  vis[u] = true;
  for (edg* e = EA[u]; e; e = e->e) {
    if (!vis[e->v]) dfs(e->v);
    for (int i = 0; i < N; ++i) f[u][i] = moc(f[u][i] + f[e->v][i]);
  }
}
inline unsigned long long det() {
  int i, j, k;
  unsigned long long s = 1, q;
  for (i = 0; i < N; ++i) {
    for (j = i; j < N && !a[j][i]; ++j)
      ;
    if (j == N)
      return 0;
    else if (i != j) {
      s = moc(P - s);
      for (k = i; k < N; ++k) std::swap(a[i][k], a[j][k]);
    }
    s = mod(s * a[i][i]);
    for (j = i + 1; j < N; ++j)
      if (a[j][i]) {
        q = mod(a[j][i] * inv(a[i][i]));
        for (k = i; k < N; ++k) a[j][k] = moc(a[j][k] - mod(q * a[i][k]) + P);
      }
  }
  return s;
}
int main() {
  scanf("%d %d %u", &V, &E, &P);
  while (E--) scanf("%d %d", &u, &v), edg_ins(u, v), ++d[v];
  N = 0;
  for (u = 1; u <= V; ++u)
    if (!EA[u]) f[u][N++] = 1;
  for (u = 1; u <= V; ++u)
    if (!vis[u]) dfs(u);
  k = 0;
  for (u = 1; u <= V; ++u)
    if (!d[u]) {
      for (i = 0; i < N; ++i) a[k][i] = f[u][i];
      ++k;
    }
  printf(
      "%llu"
      "\n",
      det());
  return 0;
}
