#include <bits/stdc++.h>
using namespace std;
template <typename T>
T abs(T a) {
  return a < 0 ? -a : a;
}
template <typename T>
T sqr(T a) {
  return a * a;
}
const int INF = (int)1e9;
const long double EPS = 1e-9;
const long double PI = 3.1415926535897932384626433832795;
const int N = 1000;
int M;
inline int modPow(int a, int p) {
  int r = 1;
  while (p > 0) {
    if (p & 1) {
      r = (r * 1ll * a) % M;
    }
    a = (a * 1ll * a) % M;
    p = (p >> 1);
  }
  return r;
}
inline int modRev(int a) { return modPow(a, M - 2); }
int n, m;
vector<int> g[N];
int dIn[N];
vector<int> v1, v2;
int z[N][N];
inline int solve(int v, int u) {
  if (v == u) return 1;
  if (z[v][u] == -1) {
    int& sum = z[v][u];
    sum = 0;
    for (int i = 0; i < int(int((g[v]).size())); ++i) {
      sum = (sum + solve(g[v][i], u)) % M;
    }
  }
  return z[v][u];
}
int a[N][N];
int gauss(int n) {
  int mul = 1;
  for (int i = 0; i < int(n); ++i) {
    int c = -1;
    for (int j = i; j < n; ++j) {
      if (a[j][i] != 0) {
        c = j;
        break;
      }
    }
    if (c == -1) return 0;
    if (c != i) {
      mul = M - mul;
      for (int j = 0; j < int(n); ++j) swap(a[i][j], a[c][j]);
    }
    int rv = modRev(a[i][i]);
    mul = (mul * 1ll * a[i][i]) % M;
    for (int j = 0; j < int(n); ++j) a[i][j] = (a[i][j] * 1ll * rv) % M;
    for (int j = i + 1; j < n; ++j) {
      if (a[j][i] == 0) continue;
      int ml = a[j][i];
      for (int k = i; k < n; ++k)
        a[j][k] = (a[j][k] - (ml * 1ll * a[i][k]) % M + M) % M;
    }
  }
  return mul;
}
int main() {
  cin >> n >> m >> M;
  for (int i = 0; i < int(m); ++i) {
    int x, y;
    scanf("%d %d", &x, &y);
    x--;
    y--;
    g[x].push_back(y);
    dIn[y]++;
  }
  for (int i = 0; i < int(n); ++i) {
    if (dIn[i] == 0) v1.push_back(i);
    if (int((g[i]).size()) == 0) v2.push_back(i);
  }
  memset(z, -1, sizeof(z));
  for (int i = 0; i < int(int((v1).size())); ++i)
    for (int j = 0; j < int(int((v2).size())); ++j)
      a[i][j] = solve(v1[i], v2[j]);
  cout << gauss(int((v1).size())) << endl;
  return 0;
}
