#include <bits/stdc++.h>
using namespace std;
int ind[1200], outd[1200], from[100000], to[100000], idx[600], src[600],
    sink[600], cntt, topo[1200], ways[1200], p;
long long mat[600][600];
vector<int> mp[1200];
bool vst[1200];
void dfs(int k) {
  vst[k] = true;
  for (size_t i = 0; i < mp[k].size(); ++i)
    if (!vst[mp[k][i]]) dfs(mp[k][i]);
  topo[--cntt] = k;
}
long long pw(long long a, int b) {
  long long ret = 1, mul = a;
  while (b) {
    if (b & 1) (ret *= mul) %= p;
    (mul *= mul) %= p;
    b >>= 1;
  }
  return ret;
}
int main() {
  int n, m;
  scanf("%d%d%d", &n, &m, &p);
  for (int i = 0; i < m; ++i) {
    scanf("%d%d", from + i, to + i);
    --from[i];
    --to[i];
    ++outd[from[i]];
    ++ind[to[i]];
  }
  int cnt = 0;
  for (int i = 0; i < n; ++i) {
    idx[i] = cnt++;
    if (!ind[i] && !outd[i]) {
      mp[cnt - 1].push_back(cnt);
      ++cnt;
    }
  }
  for (int i = 0; i < m; ++i) mp[idx[from[i]]].push_back(idx[to[i]]);
  n = cnt;
  memset(ind, 0, sizeof(ind));
  memset(outd, 0, sizeof(outd));
  for (int i = 0; i < n; ++i)
    for (size_t j = 0; j < mp[i].size(); ++j) {
      ++outd[i];
      ++ind[mp[i][j]];
    }
  cnt = 0;
  int sk = 0;
  for (int i = 0; i < n; ++i) {
    if (!ind[i]) src[cnt++] = i;
    if (!outd[i]) sink[sk++] = i;
  }
  cntt = n;
  for (int i = 0; i < n; ++i)
    if (!vst[i]) dfs(i);
  for (int i = 0; i < cnt; ++i) {
    memset(ways, 0, sizeof(ways));
    ways[src[i]] = 1;
    for (int j = 0; j < n; ++j) {
      int u = topo[j];
      for (size_t k = 0; k < mp[u].size(); ++k)
        (ways[mp[u][k]] += ways[u]) %= p;
    }
    for (int j = 0; j < cnt; ++j) mat[j][i] = ways[sink[j]];
  }
  long long ans = 1;
  for (int i = 0; i < cnt; ++i) {
    int j = i;
    while (j < cnt && !mat[j][i]) ++j;
    if (j != i) (ans *= p - 1) %= p;
    for (int k = 0; k < cnt; ++k) swap(mat[i][k], mat[j][k]);
    (ans *= mat[i][i]) %= p;
    for (int j = i + 1; j < cnt; ++j)
      if (mat[j][i]) {
        long long inv = pw(mat[i][i], p - 2);
        for (int k = cnt - 1; k >= i; --k) {
          mat[j][k] += p - (((mat[i][k] * inv) % p) * mat[j][i]) % p;
          if (mat[j][k] >= p) mat[j][k] -= p;
        }
      }
  }
  printf("%lld\n", ans);
  return 0;
}
