#include <bits/stdc++.h>   using namespace std;   inline long long read() {     long long res = 0, f = 1;     char c = getchar();     while (c <  0  || c >  9 ) {       if (c ==  - ) f = -1;       c = getchar();     }     while (c >=  0  && c <=  9 ) {       res = res * 10 + c - 48;       c = getchar();     }     return res * f;   }   const long long N = 610;   long long f[N][N];   long long a[N][N];   long long n, m, mod;   long long in[N], o[N], s[N], t[N], tots, tott, tot;   long long head[N], to[N * N], nxt[N * N], res;   queue<long long> q;   long long id[N];   long long qpow(long long a, long long b) {     long long res = 1;     while (b) {       if (b & 1) res = res * a % mod;       a = a * a % mod;       b >>= 1;     }     return res;   }   void add(long long x, long long y) {     nxt[++tot] = head[x];     head[x] = tot;     to[tot] = y;   }   void guess(long long n) {     res = 1;     for (long long i = 1; i <= n; ++i) {       for (long long j = i + 1; j <= n; ++j) {         while (a[j][i]) {           long long v = a[i][i] * qpow(a[j][i], mod - 2) % mod;           for (long long k = i; k <= n; ++k)             a[i][k] = ((a[i][k] - 1LL * v * a[j][k] % mod) + mod) % mod;           swap(a[i], a[j]), res = -res;         }       }     }     for (long long i = 1; i <= n; ++i) {       res = (1ll * res * a[i][i] % mod + mod) % mod;     }   }   signed main() {     n = read(), m = read(), mod = read();     for (long long i = 1; i <= m; ++i) {       long long x = read(), y = read();       in[y]++, o[x]++;       add(x, y);     }     for (long long i = 1; i <= n; ++i) {       if (in[i] == 0) s[++tots] = i;       if (o[i] == 0) t[++tott] = i;     }     for (long long i = 1; i <= n; ++i)       if (!in[i]) q.push(i);     tot = 0;     while (q.size()) {       long long u = q.front();       q.pop();       id[++tot] = u;       for (long long i = head[u]; i; i = nxt[i]) {         long long v = to[i];         --in[v];         if (!in[v]) q.push(v);       }     }     for (long long i = 1; i <= n; ++i) {       long long u = id[i];       f[u][u] = 1;       for (long long j = i; j <= n; ++j) {         long long v = id[j];         for (long long k = head[v]; k; k = nxt[k]) {           long long vv = to[k];           f[u][vv] = (f[u][vv] + f[u][v]) % mod;         }       }     }     for (long long i = 1; i <= tots; ++i) {       for (long long j = 1; j <= tott; ++j) {         a[i][j] = f[s[i]][t[j]];       }     }     guess(tots);     cout << res << endl;     return 0;   }   