#include <bits/stdc++.h>
using namespace std;
int ecnt;
struct Edge {
  int to;
  Edge *next;
} * mat[610], edges[100010];
void link(int x, int to) {
  edges[ecnt].to = to;
  edges[ecnt].next = mat[x];
  mat[x] = &edges[ecnt++];
}
int tol[610][610], que[610], n, m, MOD, din[610], dout[610];
int mark[610];
long long f[610][610];
long long quick_sqr(long long g, int k) {
  long long ret = 1;
  while (k) {
    if (k & 1) ret = ret * g % MOD;
    g = g * g % MOD;
    k >>= 1;
  }
  return ret;
}
int solve(int n) {
  int ret = 1;
  for (int i = 0; i < n; i++) {
    if (f[i][i] == 0) {
      ret *= -1;
      int k = i + 1;
      for (k = i + 1; k < n; k++)
        if (f[k][i]) break;
      if (k == n) return 0;
      for (int j = 0; j < n; j++) swap(f[i][j], f[k][j]);
    }
    int sc = quick_sqr(f[i][i], MOD - 2);
    for (int j = i + 1; j < n; j++) {
      long long t = f[j][i] * sc % MOD;
      for (int k = i; k < n; k++) f[j][k] = (f[j][k] - f[i][k] * t % MOD) % MOD;
    }
  }
  for (int i = 0; i < n; i++) ret = ret * f[i][i] % MOD;
  return (ret + MOD) % MOD;
}
int main() {
  scanf("%d%d%d", &n, &m, &MOD);
  while (m--) {
    int x, y;
    scanf("%d%d", &x, &y);
    x--, y--;
    link(x, y);
    din[y]++, dout[x]++;
  }
  int g = 0, h = 0;
  for (int i = 0; i < n; i++) {
    if (din[i] == 0) que[h++] = i;
    if (din[i] == 0) mark[i] |= 1;
    if (dout[i] == 0) mark[i] |= 2;
  }
  while (g < h) {
    int i = que[g++];
    for (Edge *p = mat[i]; p; p = p->next) {
      int to = p->to;
      if (--din[to] == 0) que[h++] = to;
    }
  }
  for (int i = 0; i < n; i++)
    if (mark[i] & 2) {
      tol[i][i] = 1;
      for (int t = h - 1; t >= 0; t--)
        for (Edge *p = mat[que[t]]; p; p = p->next) {
          int to = p->to;
          tol[que[t]][i] += tol[to][i];
          tol[que[t]][i] %= MOD;
        }
    }
  int xx = 0, yy = 0;
  for (int i = 0; i < n; i++)
    if (mark[i] & 1) {
      yy = 0;
      for (int j = 0; j < n; j++)
        if (mark[j] & 2) {
          f[xx][yy] = tol[i][j];
          yy++;
        }
      xx++;
    }
  printf("%d\n", solve(xx));
  return 0;
}
