#include <bits/stdc++.h>
using namespace std;
int n, m, mod;
vector<int> g[605];
bool vis[605];
int endT[605], T;
void dfs(int u) {
  vis[u] = true;
  for (int i = 0; i < (int)g[u].size(); i++) {
    int v = g[u][i];
    if (!vis[v]) dfs(v);
  }
  endT[u] = ++T;
}
pair<int, int> id[605];
int ind[605];
int dp[605][605];
int mat[605][605];
int sink[605], k1, source[605], k2, k;
int incnt[605], outcnt[605];
int mod_inverse(int x) {
  int n = mod - 2;
  int ans = 1;
  while (n) {
    if (n & 1) ans = (1ll * ans * x) % mod;
    x = (1ll * x * x) % mod;
    n >>= 1;
  }
  return ans;
}
int Gauss_Jordan() {
  bool neg = 0;
  for (int i = 1; i <= k; i++) {
    for (int j = i + 1; j <= k; j++) {
      if (mat[j][i]) {
        neg ^= 1;
        for (int l = 1; l <= k; l++) {
          swap(mat[i][l], mat[j][l]);
        }
      }
    }
    for (int j = i + 1; j <= k; j++) {
      int coef = 1ll * mat[j][i] * mod_inverse(mat[i][i]) % mod;
      coef = mod - coef;
      for (int l = i; l <= k; l++) {
        mat[j][l] = (mat[j][l] + 1ll * coef * mat[i][l]) % mod;
      }
    }
  }
  int ans = 1;
  for (int i = 1; i <= k; i++) ans = (1ll * ans * mat[i][i]) % mod;
  if (neg) ans = (mod - ans) % mod;
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n >> m >> mod;
  for (int i = 1; i <= m; i++) {
    int u, v;
    cin >> u >> v;
    g[u].push_back(v);
    incnt[v]++;
    outcnt[u]++;
  }
  for (int i = 1; i <= n; i++) {
    if (!outcnt[i]) sink[++k1] = i;
    if (!incnt[i]) source[++k2] = i;
  }
  k = k1;
  for (int i = 1; i <= n; i++)
    if (!vis[i]) dfs(i);
  for (int i = 1; i <= n; i++) id[i] = make_pair(endT[i], i);
  sort(id + 1, id + 1 + n);
  reverse(id + 1, id + 1 + n);
  for (int i = 1; i <= n; i++) ind[i] = id[i].second;
  for (int i = 1; i <= n; i++) {
    dp[ind[i]][ind[i]] = 1;
    if (!g[ind[i]].size()) continue;
    for (int j = 0; j < (int)g[ind[i]].size(); j++) {
      int v = g[ind[i]][j];
      for (int k = 1; k <= i; k++)
        dp[ind[k]][v] = ((long long)dp[ind[k]][v] + dp[ind[k]][ind[i]]) % mod;
    }
  }
  for (int i = 1; i <= k2; i++) {
    for (int j = 1; j <= k1; j++) {
      mat[i][j] = dp[source[i]][sink[j]];
    }
  }
  cout << Gauss_Jordan() << endl;
  return 0;
}
