#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 100005, N = 610;
long long read() {
  long long x = 0, flag = 1;
  char c;
  while ((c = getchar()) < '0' || c > '9')
    if (c == '-') flag = -1;
  while (c >= '0' && c <= '9')
    x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
  return x * flag;
}
long long n, m, mod, tot, f[605], in[605], out[605];
long long t[605][605];
bool vis[605];
vector<long long> fr, to;
struct edge {
  long long v, next;
} e[MAXN];
struct Matrix {
  long long n, m, sum[N][N];
  Matrix() { memset(sum, 0, sizeof(sum)), n = m = 0; }
  long long determinant() {
    if (n != m) {
      printf("Matrix '|A|' operation Error");
      exit(0);
    }
    long long a[N][N], i, j, k, t, f = 0;
    for (i = 1; i <= n; i++)
      for (j = 1; j <= n; j++) a[i][j] = (sum[i][j] % mod + mod) % mod;
    for (i = 1; i <= n; i++) {
      t = i;
      for (j = i + 1; j <= n; j++)
        if (a[j][i] && (a[j][i] < a[t][i] || !a[t][i])) t = j;
      if (a[t][i] == 0) return 0;
      if (t != i) swap(a[t], a[i]), f ^= 1;
      for (j = i + 1; j <= n; j++) {
        if (a[j][i] > a[i][i]) swap(a[j], a[i]), f ^= 1;
        while (a[j][i]) {
          t = a[i][i] / a[j][i];
          for (k = i; k <= n; k++)
            a[i][k] = ((a[i][k] - a[j][k] * t) % mod + mod) % mod;
          swap(a[i], a[j]), f ^= 1;
        }
      }
    }
    t = (f == 0 ? 1 : mod - 1);
    for (i = 1; i <= n; i++) t = (t * a[i][i]) % mod;
    return t;
  }
} T;
void dfs(long long u) {
  vis[u] = 1;
  if (!out[u]) t[u][u] = 1;
  for (long long i = f[u]; i; i = e[i].next) {
    long long v = e[i].v;
    if (!vis[v]) dfs(v);
    for (long long i = 0; i < to.size(); i++)
      t[u][to[i]] = (t[u][to[i]] + t[v][to[i]]) % mod;
  }
}
int main() {
  n = read();
  m = read();
  mod = read();
  for (long long i = 1; i <= m; i++) {
    long long u = read(), v = read();
    e[++tot] = edge{v, f[u]}, f[u] = tot;
    in[v]++;
    out[u]++;
  }
  for (long long i = 1; i <= n; i++) {
    if (!in[i]) fr.push_back(i);
    if (!out[i]) to.push_back(i);
  }
  for (long long i = 1; i <= n; i++)
    if (!in[i]) dfs(i);
  for (long long i = 0; i < fr.size(); i++)
    for (long long j = 0; j < to.size(); j++)
      T.sum[i + 1][j + 1] = t[fr[i]][to[j]];
  T.n = T.m = fr.size();
  cout << T.determinant() << endl;
}
