#include <bits/stdc++.h>
using namespace std;
int N, M, P, K;
long long an[600][600];
int ik1[600];
int ik2[600];
vector<int> a1[600];
vector<int> a2[600];
long long dp[600][600];
void erweuklid(long long a, long long b, long long &c, long long &d,
               long long &e) {
  if (b == 0) {
    c = a;
    d = 1;
    e = 0;
  } else {
    erweuklid(b, a % b, c, e, d);
    e -= d * (a / b);
  }
}
long long inv(long long a) {
  long long c, d, e;
  erweuklid(a, P, c, d, e);
  assert(c == 1);
  return (d + P) % P;
}
bool vis[600];
stack<int> top;
void visit(int i) {
  if (vis[i]) return;
  vis[i] = true;
  for (typeof((a1[i]).begin()) k = (a1[i]).begin(); k != (a1[i]).end(); k++)
    visit(*k);
  top.push(i);
}
int main() {
  scanf("%d%d%d", &N, &M, &P);
  for (int i = 0; i < M; i++) {
    int a, b;
    scanf("%d %d", &a, &b);
    a--;
    b--;
    a1[a].push_back(b);
    a2[b].push_back(a);
  }
  int ak1 = 0, ak2 = 0;
  for (int i = 0; i < N; i++) {
    ik1[i] = ik2[i] = -1;
    if (!a2[i].size()) ik1[i] = ak1++;
    if (!a1[i].size()) ik2[i] = ak2++;
  }
  K = ak1;
  for (int a = 0; a < N; a++) {
    for (int b = 0; b < N; b++) vis[b] = false;
    visit(a);
    dp[a][a] = 1;
    while (!top.empty()) {
      int i = top.top();
      top.pop();
      for (typeof((a1[i]).begin()) k = (a1[i]).begin(); k != (a1[i]).end();
           k++) {
        dp[a][*k] += dp[a][i];
        dp[a][*k] %= P;
      }
    }
    for (int b = 0; b < N; b++)
      if (ik1[a] != -1 && ik2[b] != -1) {
        an[ik1[a]][ik2[b]] += dp[a][b];
        an[ik1[a]][ik2[b]] %= P;
      }
  }
  long long det = 1;
  for (int k = 0; k < K; k++) {
    int a = -1;
    for (int aa = k; aa < K; aa++)
      if (an[aa][k] != 0) a = aa;
    if (a == -1) {
      det = 0;
      break;
    }
    if (a != k) det = -det;
    for (int b = 0; b < K; b++) swap(an[a][b], an[k][b]);
    det = det * an[k][k] % P;
    long long in = inv(an[k][k]);
    for (int as = k + 1; as < K; as++) {
      for (int b = k + 1; b < K; b++)
        an[as][b] = ((an[as][b] - an[k][b] * an[as][k] % P * in) % P + P) % P;
      an[as][k] = 0;
    }
  }
  det %= P;
  det += P;
  det %= P;
  printf("%I64d\n", det);
  return 0;
}
