#include <bits/stdc++.h>
using namespace std;
int n, m, p;
vector<int> edge[610];
int a[610][610];
int u[610], v[610], in[610], out[610];
int k, id[610], d[610];
int fpow(int a, int x) {
  int ret = 1;
  for (; x; x >>= 1) {
    if (x & 1) ret = 1ll * ret * a % p;
    a = 1ll * a * a % p;
  }
  return ret;
}
int gauss(int n) {
  int mark = 0;
  for (int i = 1; i <= n; ++i) {
    int j;
    for (j = i; j <= n; ++j)
      if (a[j][i]) break;
    if (j != i) mark ^= 1;
    for (int k = 1; k <= n; ++k) swap(a[j][k], a[i][k]);
    int x = fpow(a[i][i], p - 2);
    for (int j = i + 1; j <= n; ++j) {
      int t = 1ll * a[j][i] * x % p;
      for (int k = i; k <= n; ++k) a[j][k] = (a[j][k] - 1ll * t * a[i][k]) % p;
    }
  }
  int ans = 1;
  for (int i = 1; i <= n; ++i) ans = 1ll * ans * a[i][i] % p;
  if (mark) ans = -ans;
  if (ans < 0) ans += p;
  return ans;
}
int main() {
  scanf("%d%d%d", &n, &m, &p);
  for (int i = 1; i <= m; ++i) {
    int u, v;
    scanf("%d%d", &u, &v);
    edge[u].push_back(v);
    ++out[u], ++in[v];
  }
  k = 0;
  for (int i = 1; i <= n; ++i)
    if (!in[i]) u[++k] = i;
  k = 0;
  for (int i = 1; i <= n; ++i)
    if (!out[i]) v[++k] = i, id[i] = k;
  for (int i = 1; i <= k; ++i) {
    static int q[610], vis[610], use[610];
    int l = 0, r = 0;
    memset(d, 0, sizeof d);
    memset(vis, 0, sizeof vis);
    memset(use, 0, sizeof use);
    q[r++] = u[i];
    d[u[i]] = 1;
    vis[u[i]] = 1;
    while (l < r) {
      int u = q[l++];
      for (auto v : edge[u])
        if (++use[v], !vis[v]) q[r++] = v, vis[v] = 1;
    }
    l = 0, r = 0;
    q[r++] = u[i];
    while (l < r) {
      int u = q[l++];
      if (!out[u]) a[i][id[u]] = d[u];
      for (auto v : edge[u]) {
        (d[v] += d[u]) %= p;
        --use[v];
        if (!use[v]) q[r++] = v;
      }
    }
  }
  printf("%d\n", gauss(k));
  return 0;
}
