#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void read(T& x) {
  bool fu = 0;
  char c;
  for (c = getchar(); c <= 32; c = getchar())
    ;
  if (c == '-') fu = 1, c = getchar();
  for (x = 0; c > 32; c = getchar()) x = x * 10 + c - '0';
  if (fu) x = -x;
};
template <class T>
inline void read(T& x, T& y) {
  read(x);
  read(y);
}
template <class T>
inline void read(T& x, T& y, T& z) {
  read(x);
  read(y);
  read(z);
}
inline char getc() {
  char c;
  for (c = getchar(); c <= 32; c = getchar())
    ;
  return c;
}
int n, m, st, i, j, k, l, K, P, x, y;
int D0[610], D1[610], n0[610], n1[610];
int e[610][610];
long long a[610][610];
bool vis[610];
long long f[610];
long long dfs(int i) {
  if (i == st) return 1;
  if (vis[i]) return f[i];
  vis[i] = 1;
  long long ans = 0;
  for (int j = 1; j <= n; j++)
    if (e[j][i]) ans = (ans + dfs(j) * e[j][i]) % P;
  return f[i] = ans;
}
int main() {
  read(n, m, P);
  for (i = 1; i <= m; i++) read(x, y), e[x][y]++, D0[x]++, D1[y]++;
  for (i = 1; i <= n; i++)
    if (D0[i] == 0) n0[i] = ++n0[0];
  for (i = 1; i <= n; i++)
    if (D1[i] == 0) n1[i] = ++n1[0];
  K = n0[0];
  for (i = 1; i <= n; i++)
    if (n1[i]) {
      memset(vis, 0, sizeof(vis));
      memset(f, 0, sizeof(f));
      st = i;
      for (j = 1; j <= n; j++)
        if (n0[j]) a[n1[i]][n0[j]] = dfs(j);
    }
  long long ans = 1;
  for (i = 1; i <= K; i++) {
    for (j = i; j <= K; j++)
      if (a[j][i]) {
        if (j != i) ans = -ans;
        for (k = i; k <= K; k++) swap(a[j][k], a[i][k]);
        break;
      }
    if (a[i][i] == 0) continue;
    for (j = i + 1; j <= K; j++)
      while (a[j][i]) {
        long long res = a[j][i] / a[i][i];
        for (k = i; k <= K; k++) a[j][k] = (a[j][k] - a[i][k] * res) % P;
        if (a[j][i]) {
          ans = -ans;
          for (k = i; k <= K; k++) swap(a[j][k], a[i][k]);
        }
      }
  }
  for (i = 1; i <= K; i++) ans = ans * a[i][i] % P;
  if (ans < 0) ans += P;
  cout << ans << endl;
  return 0;
}
