#include <bits/stdc++.h>
using namespace std;
const int maxn = 603, maxm = 100003;
int n, m, p, Link[maxn], t[maxm], pre[maxm], in[maxn], out[maxn], S[maxn],
    T[maxn], s[maxn], tot = 0, flag = 0;
long long a[maxn][maxn], f[maxn][maxn], ans = 1;
int get() {
  int f = 0, v = 0;
  char ch;
  while (!isdigit(ch = getchar()))
    if (ch == '-') break;
  if (ch == '-')
    f = 1;
  else
    v = ch - 48;
  while (isdigit(ch = getchar())) v = v * 10 + ch - 48;
  if (f == 1)
    return -v;
  else
    return v;
}
long long Pow(long long a, int b) {
  long long res = 1;
  for (; b > 0; b /= 2) {
    if (b % 2 == 1) res = res * a % p;
    a = a * a % p;
  }
  return res;
}
int main() {
  n = get(), m = get(), p = get();
  for (int i = 1; i <= m; i++) {
    int x = get(), y = get();
    pre[++tot] = Link[x];
    Link[x] = tot;
    t[tot] = y;
    in[y]++, out[x]++;
  }
  tot = 0;
  for (int i = 1; i <= n; i++)
    if (in[i] == 0) s[tot] = i, S[++tot] = i, f[i][i] = 1;
  tot = 0;
  for (int i = 1; i <= n; i++)
    if (out[i] == 0) T[++tot] = i;
  int front = 0, rear = tot, N = tot;
  while (front != rear) {
    int x = s[front];
    for (int i = Link[x]; i > 0; i = pre[i]) {
      in[t[i]]--;
      if (in[t[i]] == 0) s[rear++] = t[i];
      for (int j = 1; j <= N; j++) (f[S[j]][t[i]] += f[S[j]][x]) %= p;
    }
    front++;
  }
  for (int i = 1; i <= N; i++)
    for (int j = 1; j <= N; j++) a[i][j] = f[S[i]][T[j]];
  for (int i = 1; i <= N; i++) {
    int master = 0;
    for (int j = i; j <= N; j++)
      if (a[j][i] != 0) {
        master = j;
        break;
      }
    if (master == 0) return puts("0"), 0;
    for (int j = 1; j <= N; j++) swap(a[i][j], a[master][j]);
    if (master != i) flag ^= 1;
    long long x = Pow(a[i][i], p - 2);
    ans = ans * a[i][i] % p;
    for (int j = i + 1; j <= N; j++) (a[i][j] *= x) %= p;
    for (int j = i + 1; j <= N; j++)
      for (int k = N; k >= i + 1; k--)
        a[j][k] = ((a[j][k] - a[i][k] * a[j][i]) % p + p) % p;
  }
  if (flag) ans = p - ans;
  printf("%d\n", int(ans));
  return 0;
}
