#include <bits/stdc++.h>
inline int read() {
  register int x, c;
  while (isspace(c = getchar()))
    ;
  for (x = -48 + c; isdigit(c = getchar()); (x *= 10) += c - 48)
    ;
  return x;
}
inline int mpow(int a, int x, const int P) {
  register int rt(1);
  for (; x; x >>= 1, a = 1LL * a * a % P)
    if (x & 1) {
      rt = 1LL * rt * a % P;
    }
  return rt;
}
struct edge {
  int to;
  edge* nxt;
} g[100010], *head[700], *cur = g;
int p;
int cnt[700];
int det[700][700];
int in[700], out[700];
std ::bitset<700> vis;
inline void add_edge(int u, int v) {
  *cur = (edge){v, head[u]};
  head[u] = cur++;
}
int Det(int d[][700], int n) {
  int cnt = 0, ret = 1;
  for (int i = 0; i < n; ++i) {
    int t = -1;
    for (int j = i; j < n; ++j)
      if (d[j][i]) {
        t = j;
        break;
      }
    if (t == -1) return 0;
    if (t != i) ++cnt;
    for (int j = 0; j < n; ++j) std ::swap(d[i][j], d[t][j]);
    int inv = mpow(d[i][i], p - 2, p);
    for (int j = i + 1; j < n; ++j)
      if (d[j][i]) {
        int temp = 1LL * d[j][i] * inv % p;
        for (int k = i; k < n; ++k) {
          d[j][k] = (d[j][k] - 1LL * temp * d[i][k] % p) % p;
          while (d[j][k] < 0) d[j][k] += p;
        }
      }
  }
  for (int i = 0; i < n; ++i) ret = 1LL * ret * d[i][i] % p;
  return cnt & 1 ? p - ret : ret;
}
inline void Count(int u) {
  if (vis.test(u)) return;
  vis.set(u);
  for (edge* it = head[u]; it; it = it->nxt) {
    Count(it->to);
    cnt[u] += cnt[it->to];
    if (cnt[u] >= p) cnt[u] -= p;
  }
}
std ::vector<int> s, t;
int main() {
  int n = read(), m = read();
  p = read();
  while (m--) {
    int u = read(), v = read();
    ++out[u], ++in[v];
    add_edge(v, u);
  }
  for (int i = 1; i <= n; ++i) {
    if (in[i] == 0) {
      s.push_back(i);
    }
    if (out[i] == 0) {
      t.push_back(i);
    }
  }
  assert(s.size() == t.size());
  for (std ::vector<int>::const_iterator it1 = s.begin(); it1 != s.end();
       ++it1) {
    vis.reset();
    memset(cnt, 0, sizeof cnt);
    cnt[*it1] = 1;
    vis.set(*it1);
    for (std ::vector<int>::const_iterator it2 = t.begin(); it2 != t.end();
         ++it2) {
      Count(*it2);
      det[it1 - s.begin()][it2 - t.begin()] = cnt[*it2];
    }
  }
  printf("%d\n", Det(det, (int)s.size()));
  return 0;
}
