#include <bits/stdc++.h>
using namespace std;
long long fpm(long long b, long long e, long long m) {
  long long t = 1;
  for (; e; e >>= 1, b = b * b % m) e & 1 ? t = t * b % m : 0;
  return t;
}
template <class T>
inline bool chkmin(T &a, T b) {
  return a > b ? a = b, true : false;
}
template <class T>
inline bool chkmax(T &a, T b) {
  return a < b ? a = b, true : false;
}
template <class T>
inline T sqr(T x) {
  return x * x;
}
template <class edge>
struct Graph {
  vector<vector<edge> > adj;
  Graph(int n) {
    adj.clear();
    adj.resize(n + 5);
  }
  Graph() { adj.clear(); }
  void resize(int n) { adj.resize(n + 5); }
  void add(int s, edge e) { adj[s].push_back(e); }
  void del(int s, edge e) { adj[s].erase(find(iter(adj[s]), e)); }
  int deg(int v) { return adj[v].size(); }
  vector<edge> &operator[](int t) { return adj[t]; }
};
const int maxn = 660;
Graph<int> G;
int ideg[maxn], odeg[maxn], f[maxn], rep[maxn];
long long ways[maxn][maxn];
bool reach[maxn];
int main() {
  ios_base::sync_with_stdio(false);
  int n, m, mod;
  cin >> n >> m >> mod;
  G.resize(n);
  for (int i = 1, s, t; i <= m; ++i)
    cin >> s >> t, G.add(s, t), ++odeg[s], ++ideg[t];
  vector<int> S, T;
  for (int i = 1; i <= n; ++i) {
    if (!ideg[i]) S.push_back(i);
    if (!odeg[i]) T.push_back(i);
  }
  for (int v = 0; v < (int)((S).size()); ++v) {
    memset(reach, 0, sizeof(reach));
    queue<int> Q;
    Q.push(S[v]), reach[S[v]] = 1;
    for (int u; !Q.empty(); Q.pop())
      for (auto &x : G[u = Q.front()])
        if (!reach[x]) reach[x] = true, Q.push(x);
    memset(ideg, 0, sizeof(ideg));
    for (int i = 1; i <= n; ++i)
      if (reach[i])
        for (auto &u : G[i]) ++ideg[u];
    Q.push(S[v]);
    memset(f, 0, sizeof(f)), f[S[v]] = 1;
    for (int u; !Q.empty(); Q.pop())
      for (auto &x : G[u = Q.front()])
        if (reach[x]) {
          (f[x] += f[u]) %= mod;
          if (--ideg[x] == 0) Q.push(x);
        }
    for (int u = 0; u < (int)((T).size()); ++u) ways[u][v] = f[T[u]];
  }
  int K = (int)((S).size());
  long long ans = 1;
  for (int i = 0; i < K; ++i) rep[i] = i;
  for (int i = 0, j, k; i < K; ++i) {
    for (j = i; j < K; ++j)
      if (ways[j][i] != 0) break;
    for (k = 0; k < K; ++k) swap(ways[i][k], ways[j][k]);
    ans = ans * ways[i][i] % mod;
    swap(rep[i], rep[j]);
    long long inv = fpm(ways[i][i], mod - 2, mod);
    for (j = i; j < K; ++j) ways[i][j] = ways[i][j] * inv % mod;
    for (j = K - 1; j > i; --j)
      for (k = K - 1; k > i; --k)
        ways[j][k] = (ways[j][k] + mod - ways[j][i] * ways[i][k] % mod) % mod;
  }
  for (int i = 0; i < K; ++i)
    for (int j = i + 1; j < K; ++j)
      if (rep[i] > rep[j]) ans = (mod - ans) % mod;
  cout << ans << endl;
  return 0;
}
