#include <bits/stdc++.h>
using namespace std;
long long n, m, mod, ru[2005], chu[2005], cnt, h[2005], f[605][605],
    dp[605][605], ans, tot, res;
vector<long long> S, T;
queue<long long> q;
struct l1 {
  long long to, next;
} p[400005];
long long add(long long x) { return x >= mod ? x - mod : x; }
void add(long long x, long long y) {
  cnt++;
  p[cnt].to = y;
  p[cnt].next = h[x];
  h[x] = cnt;
}
long long ksm(long long x, long long y) {
  long long ans = 1, t = x;
  while (y) {
    if (y & 1) ans = ans * t % mod;
    t = t * t % mod;
    y >>= 1;
  }
  return ans;
}
void Guess() {
  for (long long i = 1; i <= n; i++) {
    long long x = i;
    for (long long t = i + 1; t <= n; t++) {
      if (dp[t][i] > dp[x][i]) {
        x = t;
      }
    }
    if (x != i) swap(dp[x], dp[i]), res ^= 1;
    if (!dp[i][i]) return;
    for (long long t = i + 1; t <= n; t++) {
      long long now = dp[t][i] * ksm(dp[i][i], mod - 2) % mod;
      for (long long j = i; j <= n; j++) {
        dp[t][j] = (dp[t][j] - now * dp[i][j] % mod + mod) % mod;
      }
    }
  }
}
int main() {
  scanf("%lld%lld%lld", &n, &m, &mod);
  for (long long i = 1; i <= m; i++) {
    long long x, y;
    scanf("%lld%lld", &x, &y);
    add(x, y);
    ru[y]++;
    chu[x]++;
  }
  for (long long i = 1; i <= n; i++) {
    if (!ru[i]) S.push_back(i);
    if (!chu[i]) T.push_back(i);
  }
  sort(S.begin(), S.end());
  sort(T.begin(), T.end());
  tot = S.size();
  for (long long i = 0; i < tot; i++) {
    long long x = S[i];
    q.push(x);
    f[x][x] = 1;
  }
  while (!q.empty()) {
    long long x = q.front();
    q.pop();
    for (long long i = h[x]; i; i = p[i].next) {
      long long y = p[i].to;
      for (long long t = 1; t <= n; t++) {
        f[t][y] = add(f[t][y] + f[t][x]);
      }
      ru[y]--;
      if (!ru[y]) q.push(y);
    }
  }
  for (long long i = 1; i <= tot; i++) {
    long long x = S[i - 1];
    for (long long t = 1; t <= tot; t++) {
      long long y = T[t - 1];
      dp[i][t] = f[x][y];
    }
  }
  Guess();
  ans = 1;
  for (long long i = 1; i <= tot; i++) {
    ans = ans * dp[i][i] % mod;
  }
  if (res) ans = (mod - ans) % mod;
  printf("%lld", ans);
}
