#include <bits/stdc++.h>
using namespace std;
struct side {
  int a, l;
} e[100005];
int cnt[605][605], start[605], din[605], dou[605], q[605];
long long xs[605][605];
int n, m, t, tot, Mod;
inline void add(int x, int y) {
  ++dou[x];
  ++din[y];
  e[++tot].l = start[x];
  e[tot].a = y;
  start[x] = tot;
}
void Init() {
  int x, y, he = 0, ta = 0;
  scanf("%d%d%d", &n, &m, &Mod);
  for (int i = 1; i <= m; ++i) {
    scanf("%d%d", &x, &y);
    add(x, y);
  }
  for (int i = 1; i <= n; ++i) cnt[i][i] = 1;
  for (int i = 1; i <= n; ++i)
    if (!din[i]) q[++ta] = i;
  t = ta;
  while (ta != n) {
    x = q[++he];
    for (int i = start[x]; i; i = e[i].l) {
      y = e[i].a;
      for (int j = 1; j <= n; ++j) {
        cnt[j][y] += cnt[j][x];
        if (cnt[j][y] >= Mod) cnt[j][y] -= Mod;
      }
      --din[y];
      if (!din[y]) q[++ta] = y;
    }
  }
  for (int i = 1; i <= t; ++i) {
    x = 0;
    for (int j = 1; j <= n; ++j)
      if (!dou[j]) xs[i][++x] = cnt[q[i]][j];
  }
}
long long get(long long x, int k) {
  long long s = 1;
  while (k) {
    if (k & 1) s = s * x % Mod;
    k >>= 1;
    x = x * x % Mod;
  }
  return s;
}
void Work() {
  long long ans = 1;
  for (int i = 1; i <= t; ++i) {
    if (!xs[i][i]) {
      for (int j = i + 1; j <= t; ++j)
        if (xs[j][i]) {
          ans *= -1;
          for (int k = 1; k <= t; ++k) swap(xs[i][k], xs[j][k]);
          break;
        }
    }
    if (!xs[i][i]) {
      ans = 0;
      break;
    }
    for (int j = i + 1; j <= t; ++j)
      if (xs[j][i]) {
        long long c = xs[j][i];
        ans = ans * get(xs[i][i], Mod - 2) % Mod;
        for (int k = 1; k <= t; ++k) {
          xs[j][k] = xs[j][k] * xs[i][i] - xs[i][k] * c;
          xs[j][k] %= Mod;
          if (xs[j][k] < 0) xs[j][k] += Mod;
        }
      }
  }
  for (int i = 1; i <= t; ++i) ans = ans * xs[i][i] % Mod;
  if (ans < 0) ans += Mod;
  cout << ans << endl;
}
int main() {
  Init();
  Work();
  return 0;
}
