#include <bits/stdc++.h>
using namespace std;
const int Mod = (int)1e9 + 7;
const int MX = 1073741822;
const long long MXLL = 4e18;
const int Sz = 1110111;
inline void Read_rap() {
  ios_base ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
inline void randomizer3000() {
  unsigned int seed;
  asm("rdtsc" : "=A"(seed));
  srand(seed);
}
void files(string problem) {
  if (fopen((problem + ".in").c_str(), "r")) {
    freopen((problem + ".in").c_str(), "r", stdin);
    freopen((problem + ".out").c_str(), "w", stdout);
  }
}
void localInput(const char in[] = "s") {
  if (fopen(in, "r")) {
    freopen(in, "r", stdin);
  } else if (0)
    cout << "Warning: Input file not found" << endl;
}
int n, m, p;
vector<int> g[Sz], r[Sz];
long long bp(int a, int b) {
  long long res = 1;
  while (b) {
    if (b & 1) res = (res * a) % p;
    b /= 2;
    a = (a * 1ll * a) % p;
  }
  return res;
}
int determinant(vector<vector<long long> > &a, int n) {
  long long res = 1;
  for (int i = 1; i <= n; i++) {
    int k = 0;
    for (int j = i; j <= n; j++) {
      if (a[j][i]) k = j;
    }
    swap(a[i], a[k]);
    if (i != k) res = (p - res) % p;
    if (!a[i][i]) return 0;
    long long inv = bp(a[i][i], p - 2);
    res = (res * a[i][i]) % p;
    for (int j = 1; j <= n; j++) a[i][j] = (a[i][j] * inv) % p;
    for (int j = i; j <= n; j++) {
      if (j != i && a[j][i]) {
        long long C = a[j][i];
        for (int k = i; k <= n; k++) {
          a[j][k] = (a[j][k] - (a[i][k] * 1ll * C) % p + p) % p;
        }
      }
    }
  }
  return res;
}
int main() {
  Read_rap();
  cin >> n >> m >> p;
  for (int i = 1; i <= m; i++) {
    int first, second;
    cin >> first >> second;
    g[first].push_back(second);
    r[second].push_back(first);
  }
  queue<int> q;
  vector<int> deg(n + 1, 0);
  vector<vector<long long> > d(n + 1, vector<long long>(n + 1));
  for (int i = 1; i <= n; i++) {
    deg[i] = (int)(g[i].size());
    if (!deg[i]) q.push(i);
    d[i][i] = 1;
  }
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int i = 1; i <= n; i++)
      for (int to : g[v]) d[v][i] = (d[v][i] + d[to][i]) % p;
    for (int pr : r[v]) {
      deg[pr]--;
      if (deg[pr] == 0) q.push(pr);
    }
  }
  vector<int> k1, k2;
  for (int i = 1; i <= n; i++)
    if (!(int)(r[i].size())) k1.push_back(i);
  for (int i = 1; i <= n; i++)
    if (!(int)(g[i].size())) k2.push_back(i);
  vector<vector<long long> > dk((int)(k1.size()) + 1,
                                vector<long long>((int)(k1.size()) + 1));
  int k = (int)(k1.size());
  for (int i = 1; i <= k; i++)
    for (int j = 1; j <= k; j++) dk[i][j] = d[k1[i - 1]][k2[j - 1]];
  long long ans = determinant(dk, k);
  cout << ans;
  return 0;
}
