#include <bits/stdc++.h>
int head[1000], next[100005], od[1000], id[1000], source[1000], sink[1000],
    from[100005], to[100005], Sk, Se;
long long int f[1000], p;
long long int det[1000][1000];
long long int dfs(int u) {
  long long int re = 0, i;
  if (f[u] > -1) return f[u];
  for (i = head[u]; i; i = next[i]) {
    re += dfs(to[i]);
    re %= p;
  }
  f[u] = re;
  return re;
}
long long int power(long long int b, long long int a) {
  long long int s = 1, x = 1;
  a = ((a % p) + p) % p;
  for (; x <= a; x *= 2) {
    if (x & a) s = (s * b) % p;
    b = (b * b) % p;
  }
  return s;
}
int solve() {
  int n = Sk;
  long long int sign = 1, re = 1;
  int i, j, k;
  for (i = 1; i <= n; i++) {
    for (j = i; j <= n; j++)
      if (det[j][i] != 0) {
        if (i != j) {
          for (k = 1; k <= n; k++) {
            long long int tmp = det[i][k];
            det[i][k] = det[j][k];
            det[j][k] = tmp;
          }
          sign = -sign;
        }
        break;
      }
    if (j == n + 1) return 0;
    for (j = i + 1; j <= n; j++)
      if (det[j][i] > 0) {
        long long int corr = power(det[i][i], p - 2);
        corr = (corr * (p - det[j][i])) % p;
        for (k = i; k <= n; k++) det[j][k] = (det[j][k] + det[i][k] * corr) % p;
      }
  }
  for (i = 1; i <= n; i++) re = (re * det[i][i]) % p;
  return ((re * sign) % p + p) % p;
}
int main() {
  int n, m, i, k2, k, u, v;
  long long int sig = 1;
  scanf("%d%d", &n, &m);
  std::cin >> p;
  for (i = 1; i <= m; i++) {
    scanf("%d%d", &from[i], &to[i]);
    next[i] = head[from[i]];
    head[from[i]] = i;
    od[from[i]]++;
    id[to[i]]++;
  }
  for (i = 1; i <= n; i++) {
    if (od[i] == 0 && id[i] > 0) sink[++Sk] = i;
    if (id[i] == 0 && od[i] > 0) source[++Se] = i;
  }
  for (i = 1; i <= n; i++)
    if (od[i] == 0 && id[i] == 0) {
      int cnt = 0;
      for (k = 1; k <= Sk; k++)
        if (sink[k] < i) cnt++;
      for (k = 1; k <= Se; k++)
        if (source[k] < i) cnt++;
      if (cnt % 2) sig = -sig;
    }
  for (k = 1; k <= Sk; k++) {
    v = sink[k];
    memset(f, -1, sizeof(f));
    f[v] = 1;
    for (k2 = 1; k2 <= Se; k2++) {
      u = source[k2];
      det[k2][k] = dfs(u);
    }
  }
  long long int sol = solve() * sig;
  sol = (sol % p + p) % p;
  std::cout << sol;
}
