#include <bits/stdc++.h>
using namespace std;
long long gcd(long long x, long long y) { return y ? gcd(y, x % y) : x; }
long long lcm(long long x, long long y) { return x / gcd(x, y) * y; }
int exgcd(int a, int b, int &x, int &y) {
  if (!b) {
    x = 1, y = 0;
    return a;
  }
  int g = exgcd(b, a % b, y, x);
  y -= (a / b) * x;
  return g;
}
int inv(int a, int mod) {
  int x, y;
  exgcd(a, mod, x, y);
  return (x % mod + mod) % mod;
}
const int N = 605;
int n, m, k, mod, g[N][N], idgr[N], odgr[N];
bool neg;
long long ans, dp[N][N], mat[N][N];
int head, tail, que[N];
vector<int> src, snk;
void Swap(int i, int j, int fir) {
  neg = !neg;
  for (int r = fir; r <= k; r++) {
    swap(mat[i][r], mat[j][r]);
  }
}
void Minus(int i, int j, int fir) {
  if (!mat[j][fir]) return;
  long long coef = (long long)(mod - mat[j][fir]) * inv(mat[i][fir], mod) % mod;
  for (int r = fir; r <= k; r++) {
    mat[j][r] = (mat[j][r] + coef * mat[i][r]) % mod;
  }
}
int main() {
  scanf("%d%d%d", &n, &m, &mod);
  for (int i = 1; i <= m; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    g[u][v]++;
    idgr[v]++;
    odgr[u]++;
  }
  head = tail = 0;
  for (int i = 1; i <= n; i++)
    if (!idgr[i]) {
      src.push_back(i);
      que[tail++] = i;
      dp[i][i] = 1;
    }
  for (int i = 1; i <= n; i++)
    if (!odgr[i]) {
      snk.push_back(i);
    }
  assert(src.size() == snk.size());
  k = src.size();
  while (head < tail) {
    int u = que[head++];
    for (int v = 1; v <= n; v++) {
      if (g[u][v]) {
        idgr[v] -= g[u][v];
        if (!idgr[v]) que[tail++] = v;
        for (int i = 0; i < k; i++)
          dp[src[i]][v] = (dp[src[i]][v] + dp[src[i]][u] * g[u][v]) % mod;
      }
    }
  }
  for (int i = 1; i <= k; i++)
    for (int j = 1; j <= k; j++) mat[i][j] = dp[src[i - 1]][snk[j - 1]];
  neg = false;
  for (int i = 1; i <= k; i++) {
    int j = i;
    while (j <= k && !mat[j][i]) j++;
    if (j > k) break;
    if (j != i) Swap(i, j, i);
    for (j = j + 1; j <= k; j++) Minus(i, j, i);
  }
  ans = 1ll;
  for (int i = 1; i <= k; i++) ans = ans * mat[i][i] % mod;
  if (neg) ans = ans * (mod - 1) % mod;
  printf("%d\n", (int)ans);
  return 0;
}
