#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long x = 0;
  int w = 0;
  char ch = 0;
  while (ch < '0' || ch > '9') w |= ch == '-', ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
  return w ? -x : x;
}
const int N = 605;
const int M = 100005;
int n, m, vis[N][N], nedge, hed[N], s1, s2, K, S[N], T[N], ind[N], outd[N];
long long P, f[N][N], mp[N][N];
struct mat {
  long long num[N][N];
  void init() { memset(num, 0, sizeof(num)); }
} a;
struct Edge {
  int to, nex;
} edge[M];
void addedge(int a, int b) {
  edge[nedge].to = b;
  edge[nedge].nex = hed[a];
  hed[a] = nedge++;
}
void solve(int k, int nw) {
  vis[k][nw] = 1;
  for (int i = hed[k]; ~i; i = edge[i].nex) {
    int v = edge[i].to;
    if (!vis[v][nw]) solve(v, nw);
    f[k][nw] = (f[k][nw] + mp[k][v] * f[v][nw]) % P;
  }
}
void exgcd(long long a, long long b, long long c, long long &x, long long &y) {
  if (!a) {
    x = 0;
    y = c / b;
    return;
  }
  if (!b) {
    y = 0;
    x = c / a;
    return;
  }
  exgcd(b, a % b, c, y, x);
  y -= (a / b) * x;
}
long long inv(long long u) {
  long long x, y;
  u %= P;
  exgcd(u, P, 1, x, y);
  return x % P;
}
long long det(mat &a, int n) {
  long long ans = 1;
  int w = 1;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) a.num[i][j] %= P;
  for (int i = 1; i <= n; i++) {
    int r = i;
    for (int j = i; j <= n; j++)
      if (abs(a.num[j][i]) > abs(a.num[r][i])) r = j;
    for (int j = i; j <= n; j++) swap(a.num[i][j], a.num[r][j]);
    if (!a.num[i][i]) return 0;
    if (r != i) w *= (-1);
    for (int j = i + 1; j <= n; j++) {
      long long x = (a.num[j][i] * inv(a.num[i][i])) % P;
      for (int k = i; k <= n; k++)
        a.num[j][k] = (a.num[j][k] - a.num[i][k] * x) % P;
    }
  }
  for (int i = 1; i <= n; i++) ans = (ans * a.num[i][i]) % P;
  ans = ((ans * w) % P + P) % P;
  return ans;
}
int main() {
  n = read();
  m = read();
  P = read();
  nedge = 0;
  memset(hed, -1, sizeof(hed));
  for (int i = 1; i <= m; i++) {
    int a = read();
    int b = read();
    if (!mp[a][b]) addedge(a, b);
    ++mp[a][b];
    ++ind[b];
    ++outd[a];
  }
  for (int i = 1; i <= n; i++)
    if (!ind[i]) S[++s1] = i;
  for (int i = 1; i <= n; i++)
    if (!outd[i]) T[++s2] = i;
  K = s1;
  for (int i = 1; i <= n; i++) vis[i][i] = f[i][i] = 1;
  for (int i = 1; i <= K; i++)
    for (int j = 1; j <= K; j++)
      if (!vis[S[i]][T[j]]) solve(S[i], T[j]);
  for (int i = 1; i <= K; i++)
    for (int j = 1; j <= K; j++) a.num[i][j] = f[S[i]][T[j]];
  printf("%I64d\n", det(a, K));
  return 0;
}
