#include <bits/stdc++.h>
using namespace std;
const int maxn = 605;
int n, m, mod, edge[maxn][maxn], in[maxn], out[maxn], srcnum, vis[maxn];
long long path[maxn][maxn], mat[maxn][maxn];
void rec(int u) {
  if (vis[u]) return;
  vis[u] = 1;
  for (int s = 0; s < srcnum; s++)
    for (int v = 1; v <= n; v++)
      if (edge[v][u]) {
        rec(v);
        path[s][u] = (path[s][u] + path[s][v] * edge[v][u]) % mod;
      }
}
long long fsp(long long a, int p) {
  long long ret = 1;
  while (p) {
    if (p & 1) ret = ret * a % mod;
    if (p >>= 1) a = a * a % mod;
  }
  return ret;
}
int main() {
  scanf("%d %d %d", &n, &m, &mod);
  for (int i = 0, t1, t2; i < m; i++) {
    scanf("%d %d", &t1, &t2);
    edge[t1][t2]++;
    out[t1]++;
    in[t2]++;
  }
  srcnum = 0;
  for (int i = 1; i <= n; i++)
    if (in[i] == 0) {
      path[srcnum++][i] = 1;
      vis[i] = 1;
    }
  for (int i = 1; i <= n; i++) rec(i);
  for (int i = 1, cnt = 0; i <= n; i++)
    if (out[i] == 0) {
      for (int j = 0; j < srcnum; j++) mat[j][cnt] = path[j][i];
      cnt++;
    }
  long long det = 1;
  int len = srcnum, negtive = 0;
  for (int j = 0, i; j < len; j++) {
    for (i = j; i < len && !mat[i][j]; i++)
      ;
    if (i == len) {
      det = 0;
      break;
    }
    if (i != j) {
      negtive ^= 1;
      for (int k = j; k < len; k++) swap(mat[i][k], mat[j][k]);
    }
    long long Inv = fsp(mat[j][j], mod - 2);
    for (int i = j + 1; i < len; i++)
      if (mat[i][j]) {
        long long coef = Inv * (mod - mat[i][j]) % mod;
        for (int k = j; k < len; k++)
          mat[i][k] = (mat[i][k] + mat[j][k] * coef) % mod;
      }
    det = det * mat[j][j] % mod;
  }
  if (negtive && det) det = mod - det;
  cout << det << endl;
}
