#include <bits/stdc++.h>
using namespace std;
const int MAXN = 600;
int n, m, MOD, s, in[MAXN], out[MAXN], sourceIdx[MAXN], sinkIdx[MAXN],
    mat[MAXN][MAXN];
vector<int> edge[MAXN];
void init() {
  scanf("%d%d%d", &n, &m, &MOD);
  for (int i = 0; i < m; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    a--;
    b--;
    edge[a].push_back(b);
    out[a]++;
    in[b]++;
  }
  s = 0;
  for (int i = 0; i < n; i++) {
    sourceIdx[i] = in[i] ? -1 : s++;
  }
  s = 0;
  for (int i = 0; i < n; i++) {
    sinkIdx[i] = out[i] ? -1 : s++;
  }
}
inline int fix(int x) {
  if (x >= MOD) {
    return x - MOD;
  } else if (x < 0) {
    return x + MOD;
  } else {
    return x;
  }
}
int degree[MAXN], way[MAXN];
queue<int> q;
void initMat() {
  for (int o = 0; o < n; o++) {
    if (sourceIdx[o] == -1) {
      continue;
    }
    memcpy(degree, in, sizeof(int) * n);
    memset(way, 0, sizeof(int) * n);
    for (int i = 0; i < n; i++) {
      if (!degree[i]) {
        q.push(i);
      }
    }
    way[o] = 1;
    for (; !q.empty(); q.pop()) {
      int idx = q.front(), value = way[idx];
      for (vector<int>::const_iterator it = edge[idx].begin();
           it != edge[idx].end(); it++) {
        way[*it] = fix(way[*it] + value);
        if (degree[*it] == 1) {
          q.push(*it);
        } else {
          degree[*it]--;
        }
      }
    }
    for (int i = 0; i < n; i++) {
      if (sinkIdx[i] != -1) {
        mat[sourceIdx[o]][sinkIdx[i]] = way[i];
      }
    }
  }
}
inline int powMod(int a, int b, int MOD) {
  int res = 1;
  for (; b; b >>= 1, a = (long long)a * a % MOD) {
    if (b & 1) {
      res = (long long)res * a % MOD;
    }
  }
  return res;
}
inline int modInverse(int a, int MOD) { return powMod(a, MOD - 2, MOD); }
int detMod(int n, int mat[MAXN][MAXN], int MOD) {
  int res = 1;
  for (int i = 0; i < n; i++) {
    int j;
    for (j = i; j < n; j++) {
      if (mat[j][i]) {
        break;
      }
    }
    if (j == n) {
      return 0;
    }
    if (i != j) {
      res = -res;
      for (int k = 0; k < n; k++) {
        swap(mat[i][k], mat[j][k]);
      }
    }
    res = (long long)res * mat[i][i] % MOD;
    if (mat[i][i] != 1) {
      int inv = modInverse(mat[i][i], MOD);
      for (int j = i; j < n; j++) {
        mat[i][j] = (long long)mat[i][j] * inv % MOD;
      }
    }
    for (int j = i + 1; j < n; j++) {
      for (int k = n - 1; k >= i; k--) {
        mat[j][k] = fix((mat[j][k] - (long long)mat[i][k] * mat[j][i]) % MOD);
      }
    }
  }
  return fix(res);
}
int main() {
  init();
  initMat();
  printf("%d\n", detMod(s, mat, MOD));
  return 0;
}
