#include <bits/stdc++.h>
using namespace std;
int Get() {
  char c;
  while (c = getchar(), c < '0' || c > '9')
    ;
  int X = c - 48;
  while (c = getchar(), c >= '0' && c <= '9') X = X * 10 + c - 48;
  return X;
}
int N, M, Mod;
int G[600][600], P[600][600];
long long DFS(int X, int Y) {
  if (P[X][Y] != -1) return P[X][Y];
  if (X == Y) return P[X][Y] = 1;
  P[X][Y] = 0;
  for (int i = 0; i < N; i++)
    if (G[i][Y]) P[X][Y] = (DFS(X, i) * G[i][Y] + P[X][Y]) % Mod;
  return P[X][Y];
}
long long Pow(long long A, int B) {
  if (!B) return 1;
  long long T = Pow(A * A % Mod, B / 2);
  if (B & 1) T = T * A % Mod;
  return T;
}
int main() {
  N = Get();
  M = Get();
  Mod = Get();
  memset(G, 0, sizeof(G));
  static int In[600], Out[600];
  memset(In, 0, sizeof(In));
  memset(Out, 0, sizeof(Out));
  for (int i = 0; i < M; i++) {
    int X = Get() - 1, Y = Get() - 1;
    G[X][Y]++;
    In[Y]++;
    Out[X]++;
  }
  memset(P, -1, sizeof(P));
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++) DFS(i, j);
  int K0 = 0, K1 = 0;
  static int X[600], Y[600];
  for (int i = 0; i < N; i++) {
    if (!In[i]) X[K0++] = i;
    if (!Out[i]) Y[K1++] = i;
  }
  int K = K0;
  static long long A[600][600];
  for (int i = 0; i < K; i++)
    for (int j = 0; j < K; j++) A[i][j] = P[X[i]][Y[j]];
  int Ans = 1;
  for (int i = 0; i < K; i++) {
    int P = i;
    while (P < K && !A[P][i]) P++;
    if (P == K) {
      printf("0\n");
      return 0;
    }
    for (int j = i; j < K; j++) swap(A[i][j], A[P][j]);
    if (P != i) Ans = -Ans;
    for (int j = i + 1; j < K; j++) {
      int T = A[j][i] * Pow(A[i][i], Mod - 2) % Mod;
      for (int k = i; k < K; k++) {
        A[j][k] = (A[j][k] - T * A[i][k]) % Mod;
        if (A[j][k] < 0) A[j][k] += Mod;
      }
    }
  }
  for (int i = 0; i < K; i++) Ans = (long long)Ans * A[i][i] % Mod;
  if (Ans < 0) Ans += Mod;
  printf("%d\n", Ans);
  return 0;
}
