#include <bits/stdc++.h>
using namespace std;
namespace {
int mod;
inline int Add(int x) { return x >= mod ? x - mod : x; }
inline int Sub(int x) { return x < 0 ? x + mod : x; }
inline int Mul(int x, int y) { return (long long)x * y % mod; }
int Pow(int x, int y) {
  int z = 1;
  for (; y; y >>= 1) {
    if (y & 1) z = Mul(z, x);
    x = Mul(x, x);
  }
  return z;
}
inline void Add(int &x, int y) {
  x += y;
  if (x >= mod) x -= mod;
}
inline void Sub(int &x, int y) {
  x -= y;
  if (x < 0) x += mod;
}
}  // namespace
const int maxn = 600, maxk = 600;
int n, m, k;
int mat[maxk][maxk];
int g[maxn][maxn];
int ord[maxn], deg[maxn];
int src[maxk], sink[maxk];
int dp[maxn];
void TopoSort(void) {
  int k2 = 0;
  for (int i = 0; i < n; ++i) {
    bool in = false, out = false;
    for (int j = 0; j < n; ++j) {
      if (g[j][i]) in = true;
      if (g[i][j]) out = true;
    }
    if (!in) src[k++] = i;
    if (!out) sink[k2++] = i;
  }
  queue<int> q;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      deg[i] += g[j][i];
    }
    if (!deg[i]) q.push(i);
  }
  int top = 0;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    ord[top++] = u;
    for (int v = 0; v < n; ++v) {
      if (g[u][v] && (deg[v] -= g[u][v]) == 0) {
        q.push(v);
      }
    }
  }
}
int Det(void) {
  int n = k, ans = 1;
  for (int i = 0; i < n; ++i) {
    int x = -1;
    for (int j = i; j < n; ++j) {
      if (mat[j][i]) {
        x = j;
        break;
      }
    }
    if (x == -1) return 0;
    ans = Mul(ans, mat[x][i]);
    if (x != i) ans = Sub(-ans);
    for (int j = n - 1; j >= i; --j) {
      mat[x][j] = Mul(mat[x][j], Pow(mat[x][i], mod - 2));
      swap(mat[x][j], mat[i][j]);
    }
    for (int j = 0; j < n; ++j) {
      if (i == j) continue;
      for (int k = n - 1; k >= i; --k) {
        Sub(mat[j][k], Mul(mat[j][i], mat[i][k]));
      }
    }
  }
  return ans;
}
int main(void) {
  scanf("%d%d%d", &n, &m, &mod);
  for (int i = 0; i < m; ++i) {
    int a, b;
    scanf("%d%d", &a, &b);
    --a;
    --b;
    ++g[a][b];
  }
  TopoSort();
  for (int i = 0; i < k; ++i) {
    for (int j = 0; j < n; ++j) {
      int u = ord[j];
      dp[u] = 0;
      for (int v = 0; v < n; ++v) {
        if (g[v][u]) {
          Add(dp[u], Mul(dp[v], g[v][u]));
        }
      }
      if (u == src[i]) {
        Add(dp[u], 1);
      }
    }
    for (int j = 0; j < k; ++j) {
      mat[i][j] = dp[sink[j]];
    }
  }
  int ans = Det();
  printf("%d\n", ans);
  return 0;
}
