#include <bits/stdc++.h>
using namespace std;
const int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
const double eps = 1e-8;
void read(int &k) {
  k = 0;
  char x = getchar();
  while (x < '0' || x > '9') x = getchar();
  while (x >= '0' && x <= '9') {
    k = k * 10 - 48 + x;
    x = getchar();
  }
}
int n, m, p, x, y, in[610], out[610], MOD, l[610];
int a[610][610], ans, lis[610], tmp[610], K, dp[610][610];
long long powe(long long a, long long b) {
  long long ret = 1;
  for (; b; b >>= 1, a = a * a % MOD)
    if (b & 1) ret = ret * a % MOD;
  return ret;
}
struct graph {
  int a[610], b[200010], c[200010], p;
  int dd[610], ll, rr;
  void addedge(int x, int y) {
    c[++p] = a[x];
    a[x] = p;
    b[p] = y;
  }
  void tppx() {
    memcpy(l, in, sizeof l);
    ll = 1;
    rr = 0;
    for (int i = 1; i <= n; i++)
      if (!in[i]) dd[++rr] = i, lis[rr] = i;
    for (; ll <= rr; ll++)
      for (int i = a[dd[ll]]; i; i = c[i]) {
        l[b[i]]--;
        if (!l[b[i]]) dd[++rr] = b[i];
      }
    K = 0;
    for (int i = 1; i <= n; i++)
      if (!out[i]) tmp[++K] = i;
    for (int i = 1; i <= K; i++) dp[tmp[i]][i] = 1;
    for (int i = rr; i; i--)
      if (out[dd[i]])
        for (int j = a[dd[i]]; j; j = c[j])
          for (int k = 1; k <= K; k++)
            dp[dd[i]][k] = (dp[dd[i]][k] + dp[b[j]][k]) % MOD;
    for (int i = 1; i <= K; i++)
      for (int j = 1; j <= K; j++) ::a[i][j] = dp[lis[i]][j];
  }
} A;
int main() {
  scanf("%d%d%d", &n, &m, &p);
  MOD = p;
  for (int i = 1; i <= m; i++) {
    scanf("%d%d", &x, &y);
    in[y]++;
    out[x]++;
    A.addedge(x, y);
  }
  A.tppx();
  ans = 1;
  for (int i = 1, j, k; i <= K; i++) {
    for (j = i; j <= K; j++)
      if (a[j][i]) break;
    if (j > K) {
      ans = 0;
      break;
    }
    if (j != i) {
      ans = MOD - ans;
      memcpy(a[0], a[j], sizeof a[0]);
      memcpy(a[j], a[i], sizeof a[0]);
      memcpy(a[i], a[0], sizeof a[0]);
    }
    int tmp = powe(a[i][i], MOD - 2);
    for (j = i + 1; j <= K; j++)
      for (k = K; k >= i; k--)
        a[j][k] =
            (a[j][k] - 1ll * a[i][k] * a[j][i] % MOD * tmp % MOD + MOD) % MOD;
  }
  for (int i = 1; i <= K; i++) ans = 1ll * ans * a[i][i] % MOD;
  cout << ans << endl;
  return 0;
}
