#include <bits/stdc++.h>
using namespace std;
struct edge {
  int to, next;
} e[100100];
int head[700], tot = 0, n, m, k, p;
int deg[700], num[700], f[700][700], q[700], g[700];
bool v[700];
void addedge(int x, int y) {
  e[++tot].to = y;
  e[tot].next = head[x];
  head[x] = tot;
}
void Dp(int f[], int x) {
  int i, j;
  memset(g, 0, sizeof(g));
  g[x] = 1;
  for (j = 1; j <= n; j++) {
    int x = q[j];
    for (i = head[x]; i; i = e[i].next) (g[e[i].to] += g[x]) %= p;
  }
  for (i = 1; i <= n; i++)
    if (num[i]) f[num[i]] = g[i];
}
long long Pow(long long x, int y) {
  long long ans = 1;
  while (y) {
    if (y % 2 == 1) (ans *= x) %= p;
    (x *= x) %= p;
    y /= 2;
  }
  return ans;
}
int main() {
  int i, j, k, l, x, y, h = 0, r = 0, mark;
  scanf("%d%d%d", &n, &m, &p);
  for (i = 1; i <= m; i++) {
    scanf("%d%d", &x, &y);
    deg[y]++;
    addedge(x, y);
  }
  k = 0;
  for (i = 1; i <= n; i++)
    if (deg[i] == 0) v[i] = true;
  for (i = 1; i <= n; i++)
    if (head[i] == 0) num[i] = ++k;
  for (i = 1; i <= n; i++)
    if (!deg[i]) q[++r] = i;
  while (h != r) {
    int x = q[++h];
    for (i = head[x]; i; i = e[i].next)
      if (!--deg[e[i].to]) q[++r] = e[i].to;
  }
  int tmp = 0;
  for (i = 1; i <= n; i++)
    if (v[i]) Dp(f[++tmp], i);
  mark = 1;
  for (i = 1; i <= k; i++) {
    for (l = i; l <= k; l++)
      if (f[l][i]) break;
    if (l != i) mark = -mark;
    for (j = 1; j <= k; j++) swap(f[l][j], f[i][j]);
    for (l = i + 1; l <= k; l++) {
      long long tmp = p - f[l][i] * Pow(f[i][i], p - 2) % p;
      for (j = i; j <= k; j++) (f[l][j] += f[i][j] * tmp % p) %= p;
    }
  }
  long long ans = 1;
  for (i = 1; i <= k; i++) (ans *= f[i][i]) %= p;
  if (mark == -1) ans = (p - ans) % p;
  printf("%I64d\n", ans);
  return 0;
}
