#include <bits/stdc++.h>
using namespace std;
const long long inf = 2147483647;
long long read() {
  long long x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
void print(long long x) {
  if (x < 0) putchar('-'), x = -x;
  short a[20] = {}, sz = 0;
  while (x > 0) a[sz++] = x % 10, x /= 10;
  if (sz == 0) putchar('0');
  for (long long i = sz - 1; i >= 0; i--) putchar('0' + a[i]);
}
vector<long long> adj[1111];
long long n, m, mod;
long long rd[1111], cd[1111];
vector<long long> rev[1111];
long long topo[1111];
long long dp[1111][1111];
long long from[1111];
vector<long long> src, snk;
long long aa[1111][1111];
long long k;
void Swap(long long x, long long y) {
  for (long long i = 1; i <= k; i++) {
    swap(aa[x][i], aa[y][i]);
  }
}
long long qp(long long x, long long p) {
  if (!p) return 1;
  if (p == 1) return x;
  if (p & 1) {
    long long y = qp(x, p / 2);
    return y * y % mod * x % mod;
  } else {
    long long y = qp(x, p / 2);
    return y * y % mod;
  }
}
long long inv(long long x) {
  return (long long)qp((long long)x, (long long)mod - 2);
}
long long fun() {
  long long mul = 1;
  long long now = 1;
  for (long long i = 1; i <= k; i++) {
    long long id = -1;
    for (long long j = now; j <= k; j++) {
      if (aa[j][i] != 0) {
        id = j;
        break;
      }
    }
    if (id != -1) {
      if (id != now) Swap(id, now), mul *= -1;
      long long tmp = inv(aa[now][i]);
      mul *= aa[now][i];
      mul %= mod;
      for (long long j = 1; j <= k; j++) {
        aa[now][j] = ((long long)aa[now][j] * tmp % mod);
      }
      for (long long j = now + 1; j <= k; j++) {
        if (aa[j][i] != 0) {
          long long tt = aa[j][i];
          for (long long kk = 1; kk <= k; kk++) {
            aa[j][kk] -= (1ll * tt * aa[now][kk] % mod);
            if (aa[j][kk] < 0) aa[j][kk] += mod;
          }
        }
      }
      now++;
    }
  }
  long long ans = 1;
  for (long long ii = 1; ii <= n; ii++) ans = ans * aa[ii][ii] % mod;
  ans *= mul;
  while (ans < 0) ans += mod;
  return (ans);
}
int main() {
  n = read(), m = read(), mod = read();
  for (long long i = 1; i <= m; i++) {
    long long fr = read(), tt = read();
    adj[fr].push_back(tt);
    rev[tt].push_back(fr);
    cd[fr]++;
    rd[tt]++;
  }
  vector<long long> vv;
  vv.clear();
  for (long long i = 1; i <= n; i++) {
    if (rd[i] == 0) vv.push_back(i);
    if (adj[i].empty()) snk.push_back(i);
  }
  src = vv;
  k = vv.size();
  long long cnt = 0;
  while (!vv.empty()) {
    long long x = vv.back();
    vv.pop_back();
    topo[++cnt] = x;
    from[x] = cnt;
    for (long long i = 0; i < adj[x].size(); i++) {
      rd[adj[x][i]]--;
      if (!rd[adj[x][i]]) vv.push_back(adj[x][i]);
    }
  }
  for (long long i = 1; i <= n; i++) {
    dp[i][i] = 1;
    for (long long j = i + 1; j <= n; j++) {
      for (long long kk = 0; kk < rev[topo[j]].size(); kk++) {
        if (from[rev[topo[j]][kk]] >= i) {
          dp[i][j] += dp[i][from[rev[topo[j]][kk]]];
          dp[i][j] %= mod;
        }
      }
    }
  }
  for (long long i = 1; i <= k; i++) {
    for (long long j = 1; j <= k; j++) {
      aa[i][j] = dp[from[src[i - 1]]][from[snk[j - 1]]] % mod;
    }
  }
  n = k;
  cout << fun() << endl;
  return 0;
}
