#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 10, MN = 610;
int mod, n, m;
vector<int> adj[MN], topol;
int deg[MN], mat[MN][MN], dp[MN][MN];
bool mk[MN];
void ok(int &x) {
  if (x >= mod) x -= mod;
}
int bpw(int a, int b) {
  int ret = 1, cur = a;
  while (b) {
    if (b & 1) ret = 1ll * ret * cur % mod;
    cur = 1ll * cur * cur % mod;
    b >>= 1;
  }
  return ret;
}
void dfs(int s) {
  mk[s] = true;
  for (auto v : adj[s])
    if (!mk[v]) dfs(v);
  topol.push_back(s);
}
int det(int n) {
  int ret = 1;
  for (int i = 0; i < n; ++i) {
    for (int j = i + 1; j < n; ++j) {
      if (mat[j][i] != 0) {
        ret = mod - ret;
        for (int l = 0; l < n; ++l) swap(mat[j][l], mat[i][l]);
        break;
      }
    }
    if (mat[i][i] == 0) return 0;
    ret = 1ll * ret * mat[i][i] % mod;
    int sv = bpw(mat[i][i], mod - 2);
    for (int l = i; l < n; ++l) mat[i][l] = 1ll * mat[i][l] * sv % mod;
    for (int j = i + 1; j < n; ++j)
      if (mat[j][i]) {
        int x = mat[j][i];
        for (int l = i; l < n; ++l) {
          mat[j][l] = mat[j][l] + mod - (1ll * x * mat[i][l]) % mod;
          ok(mat[j][l]);
        }
      }
  }
  return ret;
}
int main() {
  ios_base ::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> m >> mod;
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    --a, --b;
    adj[a].push_back(b);
    ++deg[b];
  }
  for (int i = 0; i < n; ++i)
    if (!mk[i]) dfs(i);
  reverse(topol.begin(), topol.end());
  for (int i = n - 1; ~i; --i)
    for (int j = i; j < n; ++j) {
      if (i == j)
        dp[topol[i]][topol[j]] = 1;
      else {
        for (auto v : adj[topol[i]]) {
          dp[topol[i]][topol[j]] += dp[v][topol[j]];
          ok(dp[topol[i]][topol[j]]);
        }
      }
    }
  int A = 0;
  for (int i = 0; i < n; ++i) {
    if (!deg[i]) {
      int B = 0;
      for (int j = 0; j < n; ++j)
        if (((int)(adj[j]).size()) == 0) mat[A][B++] = dp[i][j];
      A++;
    }
  }
  cout << det(A) << '\n';
  return 0;
}
