#include <bits/stdc++.h>
template <typename T>
inline void read(T &x) {
  x = 0;
  char c = getchar();
  bool flag = false;
  while (!isdigit(c)) {
    if (c == '-') flag = true;
    c = getchar();
  }
  while (isdigit(c)) x = x * 10 + (c ^ 48), c = getchar();
  if (flag) x = -x;
}
using namespace std;
int n, m, P;
inline long long quickpow(long long x, int k) {
  long long res = 1;
  while (k) {
    if (k & 1) res = res * x % P;
    x = x * x % P;
    k >>= 1;
  }
  return res;
}
long long e[651][651];
int ind[651], outd[651];
long long h[651][651];
int X[651], Y[651], Xtot, Ytot;
inline void Gauss() {
  long long res = 1;
  const int n = Xtot;
  for (int i = 1; i <= n; ++i) {
    int id = -1;
    for (int hang = i; hang <= n; ++hang)
      if (h[hang][i]) {
        id = hang;
        break;
      }
    if (id == -1) return puts("0"), void();
    if (id != i) swap(h[id], h[i]), res = P - res;
    const int inv = quickpow(h[i][i], P - 2);
    res = res * h[i][i] % P;
    for (int j = i; j <= n; ++j) h[i][j] = h[i][j] * inv % P;
    for (int hang = i + 1; hang <= n; ++hang) {
      long long k = h[hang][i];
      for (int j = i; j <= n; ++j) {
        h[hang][j] = (h[hang][j] - k * h[i][j]) % P;
      }
    }
  }
  printf("%lld\n", (res % P + P) % P);
}
int main() {
  read(n), read(m), read(P);
  for (int i = 1; i <= m; ++i) {
    int u, v;
    read(u), read(v);
    ++e[u][v];
    ++ind[v];
    ++outd[u];
  }
  for (int k = 1; k <= n; ++k) {
    for (int i = 1; i <= n; ++i)
      if (e[i][k]) {
        for (int j = 1; j <= n; ++j) {
          e[i][j] = (e[i][j] + e[i][k] * e[k][j]) % P;
        }
      }
  }
  for (int i = 1; i <= n; ++i) e[i][i] = 1;
  for (int i = 1; i <= n; ++i) {
    if (!ind[i]) X[++Xtot] = i;
    if (!outd[i]) Y[++Ytot] = i;
  }
  for (int i = 1; i <= Xtot; ++i) {
    for (int j = 1; j <= Ytot; ++j) {
      h[i][j] = e[X[i]][Y[j]];
    }
  }
  Gauss();
  return 0;
}
