#include <bits/stdc++.h>
using namespace std;
const int N = 600;
const int M = 1e5;
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
struct edge {
  int nxt, to;
} E[M + 5];
int hd[N + 5], et;
int in[N + 5], out[N + 5], ka[N + 5];
int que[N + 5], vl[N + 5];
int f[N + 5][N + 5];
int n, m, p, L;
int inc(int a, int b) { return a + b - (a + b >= p ? p : 0); }
int pow(int x, int a) {
  int ret = 1;
  while (a) {
    if (a & 1) ret = (long long)ret * x % p;
    x = (long long)x * x % p;
    a >>= 1;
  }
  return ret;
}
void add(int a, int b) {
  E[++et] = (edge){hd[a], b};
  hd[a] = et;
  in[b]++;
  out[a]++;
}
void topo(int *a, int x) {
  int l = 1, r = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i] == 0) que[++r] = i;
    vl[i] = i == x;
  }
  while (l <= r) {
    int u = que[l++];
    for (int i = hd[u]; i; i = E[i].nxt) {
      int to = E[i].to;
      a[to]--;
      vl[to] = inc(vl[to], vl[u]);
      if (a[to] == 0) que[++r] = to;
    }
  }
}
void Get_M() {
  int c1 = 0, c2 = 0;
  for (int i = 1; i <= n; i++) {
    if (in[i]) continue;
    c1++;
    c2 = 0;
    for (int j = 1; j <= n; j++) ka[j] = in[j];
    topo(ka, i);
    for (int j = 1; j <= n; j++)
      if (out[j] == 0) f[c1][++c2] = vl[j];
  }
  L = c1;
}
void r_swap(int a, int b) {
  for (int i = 1; i <= L; i++) swap(f[a][i], f[b][i]);
}
void r_plus(int a, int b, int c) {
  for (int i = 1; i <= L; i++)
    f[a][i] = inc(f[a][i], (long long)f[b][i] * c % p);
}
int Gauss() {
  int c = 1;
  for (int i = 1; i <= L; i++) {
    if (f[i][i] == 0) {
      for (int j = i + 1; j <= L; j++)
        if (f[j][i]) {
          r_swap(i, j);
          c = p - c;
          break;
        }
    }
    for (int j = i + 1; j <= L; j++) {
      if (!f[j][i]) continue;
      int op = p - (long long)f[j][i] * pow(f[i][i], p - 2) % p;
      r_plus(j, i, op);
    }
  }
  int ret = c;
  for (int i = 1; i <= L; i++) ret = (long long)ret * f[i][i] % p;
  return ret;
}
int main() {
  scanf("%d%d%d", &n, &m, &p);
  for (int i = 1; i <= m; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    add(a, b);
  }
  Get_M();
  int ans = Gauss();
  printf("%d\n", ans);
  return 0;
}
