#include <cstdio>
#include <cstring>
#include <algorithm>
#define FOR(i,s,e) for (int i=(s); i<(e); i++)
#define FOE(i,s,e) for (int i=(s); i<=(e); i++)
#define CLR(a, x) memset(a, x, sizeof(a))
#define LLD long long
#define N 605
#define M 100005
using namespace std;

int n, m, x, y, mod;
int u[N], p[N];
int l[N], qd[M], qn[M], dou[N], din[N], vis[N], dp[N][N], a[N], b[N];
LLD A[N][N], tmp[N];

void dfs(int x){
	if (vis[x]) return;
	vis[x] = 1;
	if (!dou[x]) dp[x][x] = 1;
	else{
		for (int i=l[x]; i; i=qn[i]){
			dfs(qd[i]);
			for (int j=1; j<=n; j++){
				dp[x][j] += dp[qd[i]][j];
				if (dp[x][j] >= mod) dp[x][j] -= mod;
			}
		}
	}
}

LLD egcd(LLD x, LLD y, LLD &a, LLD &b){
	if (!y){
		a = 1, b = 0;
		return x;
	}
	else{
		LLD t = egcd(y, x%y, b, a);
		b -= x/y * a;
		return t;
	}
}

LLD inv(LLD x, LLD m){
	LLD a, b;
	egcd(x, m, a, b);
	return (a % m + m) % m;
}

int Det(int n, LLD mod){
	int r;
	LLD t, ret = 1;

	FOR(i,0,n){
		r = -1;
		for (int j=0; j<n; j++)
		if (!u[j] && A[j][i]) r = j;
		if (r == -1) return 0;
		u[r] = i + 1;
		p[i] = r;
		ret = (ret * A[r][i]) % mod;

		t = inv(A[r][i], mod);
		FOR(j,0,n) tmp[j] = (A[r][j] * t) % mod;

		FOR(j,0,n){
			if (j < i && u[j] > u[i]) ret = mod - ret;
			if (j == r || !A[j][i] || u[j]) continue;
			t = A[j][i];
			FOR(k,0,n) A[j][k] = (A[j][k] - (tmp[k] * t) % mod + mod) % mod;
		}
	}
	return ret;
}

int main(){
	scanf("%d%d%d", &n, &m, &mod);
	for (int i=1; i<=m; i++){
		scanf("%d%d", &x, &y);
		qd[i] = y, qn[i] = l[x], l[x] = i;
		din[y]++;
		dou[x]++;
	}

	m = 0;
	for (int i=1; i<=n; i++){
		if (!din[i]) a[m++] = i;
	}
	m = 0;
	for (int i=1; i<=n; i++){
		if (!dou[i]) b[m++] = i;
	}
	sort(a, a + m);
	sort(b, b + m);

	for (int i=1; i<=n; i++) dfs(i);
	FOR(i,0,m)
	FOR(j,0,m) A[i][j] = dp[a[i]][b[j]];

	printf("%d\n", Det(m, mod));
	return 0;
}


#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
#define maxn 605
using namespace std;

int n,m,P;
int dis[maxn][maxn];
int a[maxn][maxn],k;
int info[maxn],to[maxn*maxn*2],Prev[maxn*maxn*2],cnt_e;
int in[maxn],ot[maxn];
int s[maxn],t[maxn];
int Q[maxn],L,R;

void Node(int u,int v)
{
	cnt_e++;
	Prev[cnt_e]=info[u],info[u]=cnt_e,to[cnt_e]=v;
}

int det()
{
	int ret=1;
	for(int i=1;i<=k;i++)
	{
		for(int j=i+1;j<=k;j++)
			while(a[j][i])
			{
				int t=a[i][i]/a[j][i];
				for(int p=i;p<=k;p++)
				{
					a[i][p]=(a[i][p]-1ll*t*a[j][p])%P;
					swap(a[i][p],a[j][p]);
				}
				ret=-ret;
			}
		if(!a[i][i]) return 0;
		ret=(1ll*ret*a[i][i])%P;
	}
	return (ret+P)%P;
}

int main()
{

	//freopen("1.txt","r",stdin);

	scanf("%d%d%d",&n,&m,&P);
	for(int u,v,i=1;i<=m;i++)
	{
		scanf("%d%d",&u,&v);
		Node(u,v);
		in[v]++;
		ot[u]++;
		dis[u][v]++;
	}

	for(int i=1;i<=n;i++)
	{
		dis[i][i]=1;
		if(!in[i]) s[++s[0]]=i,Q[R++]=i;
		if(!ot[i]) t[++t[0]]=i;
	}

	for(int now;L<R;)
	{
		now=Q[L++];
		for(int i=info[now];i;i=Prev[i])
		{
			in[to[i]]--;
			if(!in[to[i]]) Q[R++]=to[i];
			for(int j=1;j<=n;j++)
				if(now!=j)
					dis[j][to[i]]=(dis[j][to[i]]+dis[j][now])%P;
		}
	}

	k=s[0];
	for(int i=1;i<=k;i++)
		for(int j=1;j<=k;j++)
			a[i][j]=dis[s[i]][t[j]];

	printf("%d",det());
}


#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
#define maxn 605
using namespace std;

int n,m,p;
int dis[maxn][maxn];
int a[maxn][maxn],k;
int info[maxn],to[maxn*maxn*2],Prev[maxn*maxn*2],cnt_e;
int in[maxn],ot[maxn];
int s[maxn],t[maxn];
int Q[maxn],L,R;

void Node(int u,int v)
{
	cnt_e++;
	Prev[cnt_e]=info[u],info[u]=cnt_e,to[cnt_e]=v;
}

int det()
{
	int ret=1;
	for(int i=1;i<=k;i++)
	{
		for(int j=i+1;j<=k;j++)
			while(a[j][i])
			{
				int t=a[i][i]/a[j][i];
				for(int p=i;p<=k;p++)
				{
					a[i][p]=(a[i][p]-1ll*t*a[j][p])%p;
					swap(a[i][p],a[j][p]);
				}
				ret=-ret;
			}
		if(!a[i][i]) return 0;
		ret=(1ll*ret*a[i][i])%p;
	}
	return (ret+p)%p;
}

int main()
{
	scanf("%d%d%d",&n,&m,&p);
	for(int u,v,i=1;i<=m;i++)
	{
		scanf("%d%d",&u,&v);
		Node(u,v);
		in[v]++;
		ot[u]++;
		dis[u][v]++;
	}

	for(int i=1;i<=n;i++)
	{
		dis[i][i]=1;
		if(!in[i]) s[++s[0]]=i,Q[R++]=i;
		if(!ot[i]) t[++t[0]]=i;
	}

	for(int now;L<R;)
	{
		now=Q[L++];
		for(int i=info[now];i;i=Prev[i])
		{
			in[to[i]]--;
			if(!in[to[i]]) Q[R++]=to[i];
			for(int j=1;j<=n;j++)
				if(now!=j)
					dis[j][to[i]]=(dis[j][to[i]]+dis[j][now])%p;
		}
	}

	k=s[0];
	for(int i=1;i<=k;i++)
		for(int j=1;j<=k;j++)
			a[i][j]=dis[s[i]][t[j]];

	printf("%d",det());
}


#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<queue>
#include<set>
#define md double
#define LL long long
using namespace std;
int gi() {
	int w=0;bool q=1;char c=getchar();
	while ((c<'0'||c>'9') && c!='-') c=getchar();
	if (c=='-') q=0,c=getchar();
	while (c>='0'&&c <= '9') w=w*10+c-'0',c=getchar();
	return q? w:-w;
}
const int N=1010;
int a[N][N],pos[N];
bool rev[N],d[N];int u[N*N],v[N*N];
int main()
{
#ifndef ONLINE_JUDGE
	freopen("cf167e.in","r",stdin);
	freopen("cf167e.out","w",stdout);
#endif
	int n=gi(),m=gi(),mod=gi(),i,j,k,R=0,L=0,ret,t;
	for (i=1;i<=m;i++) u[i]=gi(),rev[v[i]=gi()]=true,d[u[i]]=d[v[i]]=true;
	for (i=1;i<=n;i++)
		if (d[i]) pos[i]=rev[i]?++R:++L;
		else a[++L][++R]=1;
	for (i=1;i<=m;i++) ++a[pos[u[i]]][pos[v[i]]];
	n>>=1;ret=1;
	for (i=1;i<=n;i++) {
		for (j=i;j<=n;j++) if (a[j][i]) break;
		if (j!=i) for (k=i,ret=mod-ret;k<=n;k++) swap(a[i][k],a[j][k]);
		for (j=i+1;j<=n;j++)
			while (a[j][i])
				for (k=i,t=a[i][i]/a[j][i];k<=n;k++) a[i][k]=(a[i][k]-1LL*t*a[j][k]%mod+mod)%mod,swap(a[i][k],a[j][k]);
	}
	for (i=1;i<=n;i++) ret=1LL*ret*a[i][i]%mod;
	printf("%d\n",ret);
	return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<queue>
#include<set>
#define md double
#define LL long long
using namespace std;
int gi() {
	int w=0;bool q=1;char c=getchar();
	while ((c<'0'||c>'9') && c!='-') c=getchar();
	if (c=='-') q=0,c=getchar();
	while (c>='0'&&c <= '9') w=w*10+c-'0',c=getchar();
	return q? w:-w;
}
const int N=1010;
int a[N][N],pos[N];
int L[N],R[N],lc[N],rc[N];
int q[N],sum[N];
int head[N],next[N*N],to[N*N];
int main()
{
#ifndef ONLINE_JUDGE
	freopen("cf167e.in","r",stdin);
	freopen("cf167e.out","w",stdout);
#endif
	int n=gi(),m=gi(),mod=gi(),i,j,k,ret,t,lt=0,rt=0,l=0,r=0,x,y;
	for (i=1;i<=m;i++) ++R[x=gi()],++L[y=gi()],next[i]=head[x],head[x]=i,to[i]=y;
	for (i=1;i<=n;i++) {
		if (!L[i]) lc[++lt]=q[++r]=i;
		if (!R[i]) rc[++rt]=i;
	}
	while (l!=r) for (i=head[k=q[++l]];i;i=next[i]) if (!--L[to[i]]) q[++r]=to[i];
	for (j=1;j<=lt;j++) {
		for (i=1;i<=n;i++) sum[i]=0;
		sum[lc[j]]=1;
		for (k=1;k<=n;k++) if (sum[q[k]]) for (i=head[q[k]];i;i=next[i]) (sum[to[i]]+=sum[q[k]])%=mod;
		for (i=1;i<=rt;i++) a[j][i]=sum[rc[i]];
	}
	n=lt;ret=1;
	for (i=1;i<=n;i++) {
		for (j=i;j<=n;j++) if (a[j][i]) break;
		if (j!=i) for (k=i,ret=mod-ret;k<=n;k++) swap(a[i][k],a[j][k]);
		for (j=i+1;j<=n;j++)
			while (a[j][i])
				for (k=i,t=a[i][i]/a[j][i],ret=mod-ret;k<=n;k++) a[i][k]=(a[i][k]-1LL*t*a[j][k]%mod+mod)%mod,swap(a[i][k],a[j][k]);
	}
	for (i=1;i<=n;i++) ret=1LL*ret*a[i][i]%mod;
	printf("%d\n",ret);
	return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<queue>
#include<set>
#define md double
#define LL long long
using namespace std;
int gi() {
	int w=0;bool q=1;char c=getchar();
	while ((c<'0'||c>'9') && c!='-') c=getchar();
	if (c=='-') q=0,c=getchar();
	while (c>='0'&&c <= '9') w=w*10+c-'0',c=getchar();
	return q? w:-w;
}
const int N=1010;
int a[N][N],pos[N];
int L[N],R[N],lc[N],rc[N];
int q[N],sum[N];
int head[N],next[N*N],to[N*N];
int main()
{
#ifndef ONLINE_JUDGE
	freopen("cf167e.in","r",stdin);
	freopen("cf167e.out","w",stdout);
#endif
	int n=gi(),m=gi(),mod=gi(),i,j,k,ret,t,lt=0,rt=0,l=0,r=0,x,y;
	for (i=1;i<=m;i++) ++L[x=gi()],++R[y=gi()],next[i]=head[x],head[x]=i,to[i]=y;
	for (i=1;i<=n;i++) {
		if (!R[i]) lc[++lt]=q[++r]=i;
		if (!L[i]) rc[++rt]=i;
	}
	while (l!=r) for (i=head[k=q[++l]];i;i=next[i]) if (!--R[to[i]]) q[++r]=to[i];
	for (j=1;j<=lt;j++) {
		for (i=1;i<=n;i++) sum[i]=0;
		sum[lc[j]]=1;
		for (k=1;k<=n;k++) if (sum[q[k]]) for (i=head[q[k]];i;i=next[i]) (sum[to[i]]+=sum[q[k]])%=mod;
		for (i=1;i<=rt;i++) a[j][i]=sum[rc[i]];
	}
	n=lt;ret=1;
	for (i=1;i<=n;i++) {
		for (j=i;j<=n;j++) if (a[j][i]) break;
		if (j!=i) for (k=i,ret=mod-ret;k<=n;k++) swap(a[i][k],a[j][k]);
		for (j=i+1;j<=n;j++)
			while (a[j][i])
				for (k=i,t=a[i][i]/a[j][i],ret=mod-ret;k<=n;k++) a[i][k]=(a[i][k]-1LL*t*a[j][k]%mod+mod)%mod,swap(a[i][k],a[j][k]);
	}
	for (i=1;i<=n;i++) ret=1LL*ret*a[i][i]%mod;
	printf("%d\n",ret);
	return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<queue>
#include<set>
#define md double
#define LL long long
using namespace std;
int gi() {
	int w=0;bool q=1;char c=getchar();
	while ((c<'0'||c>'9') && c!='-') c=getchar();
	if (c=='-') q=0,c=getchar();
	while (c>='0'&&c <= '9') w=w*10+c-'0',c=getchar();
	return q? w:-w;
}
const int N=1010;
int a[N][N],pos[N];
bool rev[N];
int u[N*N],v[N*N];
int main()
{
#ifndef ONLINE_JUDGE
	freopen("cf167e.in","r",stdin);
	freopen("cf167e.out","w",stdout);
#endif
	int n=gi(),m=gi(),mod=gi(),i,j,k,R=0,L=0,ret,t;
	for (i=1;i<=m;i++) u[i]=gi(),rev[v[i]=gi()]=true;
	for (i=1;i<=n;i++) pos[i]=rev[i]?++R:++L;
	for (i=1;i<=m;i++) ++a[pos[u[i]]][pos[v[i]]];
	n>>=1;ret=1;
	for (i=1;i<=n;i++) {
		for (j=i;j<=n;j++) if (a[j][i]) break;
		if (j!=i) for (k=i,ret=mod-ret;k<=n;k++) swap(a[i][k],a[j][k]);
		for (j=i+1;j<=n;j++)
			while (a[j][i])
				for (k=i,t=a[i][i]/a[j][i];k<=n;k++) a[i][k]=(a[i][k]-1LL*t*a[j][k]%mod+mod)%mod,swap(a[i][k],a[j][k]);
	}
	for (i=1;i<=n;i++) ret=1LL*ret*a[i][i]%mod;
	printf("%d\n",ret);
	return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <algorithm>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <cstdlib>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <climits>
//#include <ext/hash_map>

using namespace std;
using namespace __gnu_cxx;

#define REP(i,n) for(int i = 0; i < int(n); ++i)
#define REPV(i, n) for (int i = (n) - 1; (int)i >= 0; --i)
#define FOR(i, a, b) for(int i = (int)(a); i < (int)(b); ++i)

#define FE(i,t) for (__typeof((t).begin())i=(t).begin();i!=(t).end();++i)
#define FEV(i,t) for (__typeof((t).rbegin())i=(t).rbegin();i!=(t).rend();++i)

#define two(x) (1LL << (x))
#define ALL(a) (a).begin(), (a).end()

#define pb push_back
#define ST first
#define ND second
#define MP(x,y) make_pair(x, y)

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<string> vs;

template<class T> void checkmin(T &a, T b){if (b<a)a=b;}
template<class T> void checkmax(T &a, T b){if (b>a)a=b;}
template<class T> void out(T t[], int n){REP(i, n)cout<<t[i]<<" "; cout<<endl;}
template<class T> void out(vector<T> t, int n=-1){for (int i=0; i<(n==-1?t.size():n); ++i) cout<<t[i]<<" "; cout<<endl;}
inline int count_bit(int n){return (n==0)?0:1+count_bit(n&(n-1));}
inline int low_bit(int n){return (n^n-1)&n;}
inline int ctz(int n){return (n==0?-1:ctz(n>>1)+1);}
int toInt(string s){int a; istringstream(s)>>a; return a;}
string toStr(int a){ostringstream os; os<<a; return os.str();}

const int maxn=666;
int mod;
int m[maxn][maxn];
int n, ne;
int dega[maxn], degb[maxn];
vi e[maxn];
int isa[maxn], isb[maxn];
int dp[maxn][maxn];
int na;

int go(int a, int b){
    if (a==b) return 1;
    int &r=dp[a][b];
    if (r!=-1) return r;
    r=0;
    REP(i,e[a].size()) r=(r+go(e[a][i],b))%mod;
    return r;
}

int inv(int a, int p){
    int x=1;
    for (; p; p>>=1, a=1ll*a*a%mod) if (p&1) x=1ll*x*a%mod;
    return x;
}

int det(){
    int res=1;
    REP(i,na){
        int p=-1;
        FOR(j,i,na) if (m[j][i]!=0){p=j; break;}
        if (p==-1) return 0;
        if (p!=i) res=(mod-res)%mod;
        REP(j,na) swap(m[i][j],m[p][j]);
        int aa=inv(m[i][i],mod-2);
        res=1ll*res*aa%mod;
        FOR(j,i,na) m[i][j]=1ll*m[i][j]*aa%mod;
        FOR(j,i+1,na){
            int bb=m[j][i];
            FOR(k,i,na) m[j][k]-=1ll*m[j][i]*m[i][k]%mod, m[j][k]=(mod+m[j][k])%mod;
        }
    }
    return res;
}

int main(){
    cin>>n>>ne>>mod;
    REP(i,ne){
        int a, b; scanf(" %d%d", &a,&b);
        --a; --b;
        e[a].pb(b);
        ++dega[a]; ++degb[b];
    }

    vi la, lb;
    REP(i,n){
        if (!dega[i]) la.pb(i);
        if (!degb[i]) lb.pb(i);
    }
    memset(dp,-1,sizeof(dp));
    na=la.size();
    REP(i,na) REP(j,na) m[i][j]=go(lb[i],la[j]);
    cout<<det()<<endl;

    return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <algorithm>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <cstdlib>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <climits>
//#include <ext/hash_map>

using namespace std;
using namespace __gnu_cxx;

#define REP(i,n) for(int i = 0; i < int(n); ++i)
#define REPV(i, n) for (int i = (n) - 1; (int)i >= 0; --i)
#define FOR(i, a, b) for(int i = (int)(a); i < (int)(b); ++i)

#define FE(i,t) for (__typeof((t).begin())i=(t).begin();i!=(t).end();++i)
#define FEV(i,t) for (__typeof((t).rbegin())i=(t).rbegin();i!=(t).rend();++i)

#define two(x) (1LL << (x))
#define ALL(a) (a).begin(), (a).end()

#define pb push_back
#define ST first
#define ND second
#define MP(x,y) make_pair(x, y)

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<string> vs;

template<class T> void checkmin(T &a, T b){if (b<a)a=b;}
template<class T> void checkmax(T &a, T b){if (b>a)a=b;}
template<class T> void out(T t[], int n){REP(i, n)cout<<t[i]<<" "; cout<<endl;}
template<class T> void out(vector<T> t, int n=-1){for (int i=0; i<(n==-1?t.size():n); ++i) cout<<t[i]<<" "; cout<<endl;}
inline int count_bit(int n){return (n==0)?0:1+count_bit(n&(n-1));}
inline int low_bit(int n){return (n^n-1)&n;}
inline int ctz(int n){return (n==0?-1:ctz(n>>1)+1);}
int toInt(string s){int a; istringstream(s)>>a; return a;}
string toStr(int a){ostringstream os; os<<a; return os.str();}

const int maxn=666;
int mod;
int m[maxn][maxn];
int n, ne;
int dega[maxn], degb[maxn];
vi e[maxn];
int isa[maxn], isb[maxn];
int dp[maxn][maxn];
int na;

int go(int a, int b){
    if (a==b) return 1;
    int &r=dp[a][b];
    if (r!=-1) return r;
    r=0;
    REP(i,e[a].size()) r=(r+go(e[a][i],b))%mod;
    return r;
}

int inv(int a, int p){
    int x=1;
    for (; p; p>>=1, a=1ll*a*a%mod) if (p&1) x=1ll*x*a%mod;
    return x;
}

int det(){
    int res=1;
    REP(i,na){
        int p=-1;
        FOR(j,i,na) if (m[j][i]!=0){p=j; break;}
        if (p==-1) return 0;
        if (p!=i) res=(mod-res)%mod;
        REP(j,na) swap(m[i][j],m[p][j]);
        int aa=inv(m[i][i],mod-2);
        res=1ll*res*m[i][i]%mod;
        FOR(j,i,na) m[i][j]=1ll*m[i][j]*aa%mod;
        FOR(j,i+1,na){
            int bb=m[j][i];
            FOR(k,i,na) m[j][k]-=1ll*bb*m[i][k]%mod, m[j][k]=(mod+m[j][k])%mod;
        }
    }
    return res;
}

int main(){
    cin>>n>>ne>>mod;
    REP(i,ne){
        int a, b; scanf(" %d%d", &a,&b);
        --a; --b;
        e[a].pb(b);
        ++dega[a]; ++degb[b];
    }

    vi la, lb;
    REP(i,n){
        if (!dega[i]) la.pb(i);
        if (!degb[i]) lb.pb(i);
    }
    memset(dp,-1,sizeof(dp));
    na=la.size();
    REP(i,na) REP(j,na) m[i][j]=go(lb[i],la[j]);
    cout<<det()<<endl;

    return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <algorithm>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <cstdlib>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <climits>
//#include <ext/hash_map>

using namespace std;
using namespace __gnu_cxx;

#define REP(i,n) for(int i = 0; i < int(n); ++i)
#define REPV(i, n) for (int i = (n) - 1; (int)i >= 0; --i)
#define FOR(i, a, b) for(int i = (int)(a); i < (int)(b); ++i)

#define FE(i,t) for (__typeof((t).begin())i=(t).begin();i!=(t).end();++i)
#define FEV(i,t) for (__typeof((t).rbegin())i=(t).rbegin();i!=(t).rend();++i)

#define two(x) (1LL << (x))
#define ALL(a) (a).begin(), (a).end()

#define pb push_back
#define ST first
#define ND second
#define MP(x,y) make_pair(x, y)

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<string> vs;

template<class T> void checkmin(T &a, T b){if (b<a)a=b;}
template<class T> void checkmax(T &a, T b){if (b>a)a=b;}
template<class T> void out(T t[], int n){REP(i, n)cout<<t[i]<<" "; cout<<endl;}
template<class T> void out(vector<T> t, int n=-1){for (int i=0; i<(n==-1?t.size():n); ++i) cout<<t[i]<<" "; cout<<endl;}
inline int count_bit(int n){return (n==0)?0:1+count_bit(n&(n-1));}
inline int low_bit(int n){return (n^n-1)&n;}
inline int ctz(int n){return (n==0?-1:ctz(n>>1)+1);}
int toInt(string s){int a; istringstream(s)>>a; return a;}
string toStr(int a){ostringstream os; os<<a; return os.str();}

const int maxn=666;
int mod;
int m[maxn][maxn];
int n, ne;
int dega[maxn], degb[maxn];
vi e[maxn];
int isa[maxn], isb[maxn];
int dp[maxn][maxn];
int na;

int go(int a, int b){
    if (a==b) return 1;
    int &r=dp[a][b];
    if (r!=-1) return r;
    r=0;
    REP(i,e[a].size()) r=(r+go(e[a][i],b))%mod;
    return r;
}

int inv(int a, int p){
    int x=1;
    for (; p; p>>=1, a=1ll*a*a%mod) if (p&1) x=1ll*x*a%mod;
    return x;
}

int det(){
    int res=1;
    REP(i,na){
        int p=-1;
        FOR(j,i,na) if (m[j][i]!=0){p=j; break;}
        if (p==-1) return 0;
        if (p!=i) res=(mod-res)%mod;
        REP(j,na) swap(m[i][j],m[p][j]);
        int aa=inv(m[i][i],mod-2);
        res=1ll*res*aa%mod;
        FOR(j,i,na) m[i][j]=1ll*m[i][j]*aa%mod;
        FOR(j,i+1,na){
            int bb=m[j][i];
            FOR(k,i,na) m[j][k]-=1ll*bb*m[i][k]%mod, m[j][k]=(mod+m[j][k])%mod;
        }
    }
    return res;
}

int main(){
    cin>>n>>ne>>mod;
    REP(i,ne){
        int a, b; scanf(" %d%d", &a,&b);
        --a; --b;
        e[a].pb(b);
        ++dega[a]; ++degb[b];
    }

    vi la, lb;
    REP(i,n){
        if (!dega[i]) la.pb(i);
        if (!degb[i]) lb.pb(i);
    }
    memset(dp,-1,sizeof(dp));
    na=la.size();
    REP(i,na) REP(j,na) m[i][j]=go(lb[i],la[j]);
    cout<<det()<<endl;

    return 0;
}


#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
using namespace std;
#define maxn 101000
int mod;
typedef long long LL;
struct EDGE
{
    int to,nxt;
}box[maxn*2];
int h[maxn],top,f1[maxn],f2[maxn];
void add(int u,int v)
{
    box[top].to=v;
    box[top].nxt=h[u];
    h[u]=top++;
}
int dp[610][610];
int dfs(int a,int b)
{
    if(dp[a][b]+1) return dp[a][b];
    int p=h[a];
    dp[a][b]=0;
    while(p!=-1){
        dp[a][b]=(dp[a][b]+dfs(box[p].to,b))%mod;
        p=box[p].nxt;
    }
    return dp[a][b];
}
LL ni(LL i)
{
    int n=mod-2;
    LL res=1;
    while(n) {
        if(n&1) res=res*i%mod;
        i=i*i%mod;
        n>>=1;
    }
    return res;
}
int ss[610],sn[610],c1,c2;
int mat[610][610];
int main()
{
    int n,m;
    scanf("%d%d%d",&n,&m,&mod);
    memset(dp,-1,sizeof(dp));
    memset(h,-1,sizeof(h));
    for(int i=1;i<=m;i++){
        int a,b;
        scanf("%d%d",&a,&b);
        f1[a]=1;
        f2[b]=1;
        add(a,b);
    }
    for(int i=1;i<=n;i++) dp[i][i]=1;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(dp[i][j]==-1) dfs(i,j);
        }
    }
    for(int i=1;i<=n;i++){
        if(!f1[i]) sn[c1++]=i;
        if(!f2[i]) ss[c2++]=i;
    }
    for(int i=0;i<c1;i++){
        for(int j=0;j<c1;j++){
            mat[i][j]=dp[ss[i]][sn[j]];
        }
    }
    LL ans=1;
    for(int j=0;j<c1;j++){
        int r;
        for(r=j;r<c1;r++){
            if(mat[r][j]) break;
        }
        if(r==c1) { ans=0;break;}
        for(int k=j;k<c1;k++) swap(mat[j][k],mat[r][k]);
        if(r!=j) ans=-ans;
        for(int i=0;i<c1;i++){
            if(i==j) continue;
            LL a=mat[i][j],b=mat[j][j];
            for(int k=0;k<c1;k++){
                mat[i][k]=(mat[i][k]*b-mat[j][k]*a)%mod;
            }
            ans=ans*ni(b)%mod;
        }

    }
    for(int i=0;i<c1;i++) ans=(ans*mat[i][i])%mod;
    printf("%I64d\n",(ans+mod)%mod);

}


#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
using namespace std;
#define maxn 101000
int mod;
typedef long long LL;
struct EDGE
{
    int to,nxt;
}box[maxn*2];
int h[maxn],top,f1[maxn],f2[maxn];
void add(int u,int v)
{
    box[top].to=v;
    box[top].nxt=h[u];
    h[u]=top++;
}
int dp[610][610];
int dfs(int a,int b)
{
    if(dp[a][b]+1) return dp[a][b];
    int p=h[a];
    dp[a][b]=0;
    while(p!=-1){
        dp[a][b]=(dp[a][b]+dfs(box[p].to,b))%mod;
        p=box[p].nxt;
    }
    return dp[a][b];
}
LL ni(LL i)
{
    int n=mod-2;
    LL res=1;
    while(n) {
        if(n&1) res=res*i%mod;
        i=i*i%mod;
        n>>=1;
    }
    return res;
}
int ss[400],sn[400],c1,c2;
int mat[400][400];
int main()
{
    int n,m;
    scanf("%d%d%d",&n,&m,&mod);
    memset(dp,-1,sizeof(dp));
    memset(h,-1,sizeof(h));
    for(int i=1;i<=m;i++){
        int a,b;
        scanf("%d%d",&a,&b);
        f1[a]=1;
        f2[b]=1;
        add(a,b);
    }
    for(int i=1;i<=n;i++) dp[i][i]=1;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(dp[i][j]==-1) dfs(i,j);
        }
    }
    for(int i=1;i<=n;i++){
        if(!f1[i]) sn[c1++]=i;
        if(!f2[i]) ss[c2++]=i;
    }
    for(int i=0;i<c1;i++){
        for(int j=0;j<c1;j++){
            mat[i][j]=dp[ss[i]][sn[j]];
        }
    }
    LL ans=1;
    for(int j=0;j<c1;j++){
        int r;
        for(r=j;r<c1;r++){
            if(mat[r][j]) break;
        }
        if(r==c1) { ans=0;break;}
        for(int k=j;k<c1;k++) swap(mat[j][k],mat[r][k]);
        if(r!=j) ans=-ans;
        for(int i=0;i<c1;i++){
            if(i==j) continue;
            LL a=mat[i][j],b=mat[j][j];
            for(int k=0;k<c1;k++){
                mat[i][k]=(mat[i][k]*b-mat[j][k]*a)%mod;
            }
            ans=ans*ni(b)%mod;
        }

    }
    for(int i=0;i<c1;i++) ans=(ans*mat[i][i])%mod;
    printf("%I64d\n",(ans+mod)%mod);

}


#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
using namespace std;
#define maxn 101000
int mod;
typedef long long LL;
struct EDGE
{
    int to,nxt;
}box[maxn*2];
int h[maxn],top,f1[maxn],f2[maxn];
void add(int u,int v)
{
    box[top].to=v;
    box[top].nxt=h[u];
    h[u]=top++;
}
int dp[610][610];
int dfs(int a,int b)
{
    if(dp[a][b]+1) return dp[a][b];
    int p=h[a];
    dp[a][b]=0;
    while(p!=-1){
        dp[a][b]=(dp[a][b]+dfs(box[p].to,b))%mod;
        p=box[p].nxt;
    }
    return dp[a][b];
}
LL ni(LL i)
{
    int n=mod-2;
    LL res=1;
    while(n) {
        if(n&1) res=res*i%mod;
        i=i*i%mod;
        n>>=1;
    }
    return res;
}
int ss[400],sn[400],c1,c2;
int mat[400][400];
int main()
{
    int n,m;
    scanf("%d%d%d",&n,&m,&mod);
    memset(dp,-1,sizeof(dp));
    memset(h,-1,sizeof(h));
    for(int i=1;i<=m;i++){
        int a,b;
        scanf("%d%d",&a,&b);
        f1[a]=1;
        f2[b]=1;
        add(a,b);
    }
    for(int i=1;i<=n;i++) dp[i][i]=1;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(dp[i][j]==-1) dfs(i,j);
        }
    }
    for(int i=1;i<=n;i++){
        if(!f1[i]) sn[c1++]=i;
        if(!f2[i]) ss[c2++]=i;
    }
    for(int i=0;i<c1;i++){
        for(int j=0;j<c1;j++){
            mat[i][j]=dp[ss[i]][sn[j]];
        }
    }
    LL ans=1;
    for(int j=0;j<c1;j++){
        int r;
        for(r=j;r<c1;r++){
            if(mat[r][j]) break;
        }
        if(r==c1) { ans=0;break;}
        for(int k=j;k<c1;k++) swap(mat[j][k],mat[r][k]);
        for(int i=0;i<c1;i++){
            if(i==j) continue;
            int a=mat[i][j],b=mat[j][j];
            for(int k=j;k<c1;k++){
                mat[i][k]=(mat[i][k]*b-mat[j][k]*a)%mod;
                ans=ans*ni(b)%mod;
            }
        }
        ans=ans*mat[j][j]%mod;
    }
    printf("%I64d\n",(ans+mod)%mod);

}


#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
using namespace std;
#define maxn 101000
int mod;
typedef long long LL;
struct EDGE
{
    int to,nxt;
}box[maxn*2];
int h[maxn],top,f1[maxn],f2[maxn];
void add(int u,int v)
{
    box[top].to=v;
    box[top].nxt=h[u];
    h[u]=top++;
}
int dp[610][610];
int dfs(int a,int b)
{
    if(dp[a][b]+1) return dp[a][b];
    int p=h[a];
    dp[a][b]=0;
    while(p!=-1){
        dp[a][b]=(dp[a][b]+dfs(box[p].to,b))%mod;
        p=box[p].nxt;
    }
    return dp[a][b];
}
LL ni(LL i)
{
    int n=mod-2;
    LL res=1;
    while(n) {
        if(n&1) res=res*i%mod;
        i=i*i%mod;
        n>>=1;
    }
    return res;
}
int ss[400],sn[400],c1,c2;
int mat[400][400];
int main()
{
    int n,m;
    scanf("%d%d%d",&n,&m,&mod);
    memset(dp,-1,sizeof(dp));
    memset(h,-1,sizeof(h));
    for(int i=1;i<=m;i++){
        int a,b;
        scanf("%d%d",&a,&b);
        f1[a]=1;
        f2[b]=1;
        add(a,b);
    }
    for(int i=1;i<=n;i++) dp[i][i]=1;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(dp[i][j]==-1) dfs(i,j);
        }
    }
    for(int i=1;i<=n;i++){
        if(!f1[i]) sn[c1++]=i;
        if(!f2[i]) ss[c2++]=i;
    }
    for(int i=0;i<c1;i++){
        for(int j=0;j<c1;j++){
            mat[i][j]=dp[ss[i]][sn[j]];
        }
    }
    LL ans=1;
    for(int j=0;j<c1;j++){
        int r;
        for(r=j;r<c1;r++){
            if(mat[r][j]) break;
        }
        if(r==c1) { ans=0;break;}
        for(int k=j;k<c1;k++) swap(mat[j][k],mat[r][k]);
        if(r!=j) ans=-ans;
        for(int i=0;i<c1;i++){
            if(i==j) continue;
            int a=mat[i][j],b=mat[j][j];
            for(int k=j;k<c1;k++){
                mat[i][k]=(mat[i][k]*b-mat[j][k]*a)%mod;
                ans=ans*ni(b)%mod;
            }
        }
        ans=ans*mat[j][j]%mod;
    }
    printf("%I64d\n",(ans+mod)%mod);

}


#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>

using namespace std;

const int maxn = 610, maxm = 200010, maxs = 310;
int N, M, P;
int tail(0), g1[maxn], s[maxm], next[maxm], in[maxn], ou[maxn], d[maxn];
int a[maxs][maxs], b[maxs][maxs];

int get(int a, int b)
{
	int ans(1), te(a);
	while (b)
	{
		if (b & 1) ans = (long long)ans * te % P;
		te = te * (long long)te % P;
		b >>= 1;
	}
	return ans;
}

void gauss(const int &n)
{
	long long ans = 1;
	for (int i1 = 1; i1 <= n; ++i1)
		{
			for (int j1 = 1; j1 <= n; ++j1) cerr << b[i1][j1] << ' ';
			cerr << endl;
		}
	for (int i = 1; i <= n; ++i)
	{
		int k(0), temp1(0);
		for (int j = i; j <= n; ++j)
			if (b[j][i] > k)
				k = b[j][i], temp1 = j;
		if (!k)
		{
			cout << 0 << endl;
			return;
		}
		if (temp1 != i)
		{
			for (int j = i; j <= n; ++j) swap(b[i][j], b[temp1][j]);
			ans *= -1;
		}
		for (int i1 = 1; i1 <= n; ++i1)
		{
			for (int j1 = 1; j1 <= n; ++j1) cerr << b[i1][j1] << ' ';
			cerr << endl;
		}
		for (int j = i + 1; j <= n; ++j) if (a[j][i])
		{
			ans = ans * get(b[i][i], P - 2) % P;
			if (ans < 0) ans += P;
			for (k = i + 1; k <= n; ++k)
			{
				b[j][k] = (b[i][k] * (-b[j][i]) + b[j][k] * b[i][i]) % P;
				if (b[j][k] < 0) b[j][k] += P;
			}
			b[j][i] = 0;
		}
		for (int i1 = 1; i1 <= n; ++i1)
		{
			for (int j1 = 1; j1 <= n; ++j1) cerr << b[i1][j1] << ' ';
			cerr << endl;
		}
		ans = ans * b[i][i] % P;
	}
	if (ans < 0) ans += P;
	cout << ans << endl;
}

int main()
{
	scanf("%d%d%d", &N, &M, &P);
	for (int i = 0; i < M; ++i)
	{
		int a, b;
		scanf("%d%d", &a, &b);
		s[++tail] = a; next[tail] = g1[b]; g1[b] = tail;
		++in[a], --ou[b];
	}
	int n(0), closed(1), open(0);
	for (int i = 1; i <= N; ++i) if (!in[i])
	{
		d[++open] = i;
		a[i][open] = 1;
	}
	n = open;
	for (; closed <= open; ++closed)
	{
		int t = d[closed];
		for (int temp = g1[t]; temp; temp = next[temp])
		{
			int q = s[temp];
			for (int j = 1; j <= n; ++j)
			{
				a[q][j] += a[t][j];
				if (a[q][j] >= P) a[q][j] -= P;
			}
			--in[q];
			if (!in[q]) d[++open] = q;
		}
	}
	int s1(0);
	for (int i = 1; i <= N; ++i) if (!ou[i])
	{
		++s1;
		for (int j = 1; j <= n; ++j) b[s1][j] = a[i][j];
	}
	gauss(n);
}


#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>

using namespace std;

const int maxn = 610, maxm = 200010, maxs = 310;
int N, M, P;
int tail(0), g1[maxn], s[maxm], next[maxm], in[maxn], ou[maxn], d[maxn];
int a[maxs][maxs], b[maxs][maxs];

int get(int a, int b)
{
	int ans(1), te(a);
	while (b)
	{
		if (b & 1) ans = (long long)ans * te % P;
		te = te * (long long)te % P;
		b >>= 1;
	}
	return ans;
}

void gauss(const int &n)
{
	long long ans = 1;
	for (int i1 = 1; i1 <= n; ++i1)
		{
			for (int j1 = 1; j1 <= n; ++j1) cerr << b[i1][j1] << ' ';
			cerr << endl;
		}
	for (int i = 1; i <= n; ++i)
	{
		int k(0), temp1(0);
		for (int j = i; j <= n; ++j)
			if (b[j][i] > k)
				k = b[j][i], temp1 = j;
		if (!k)
		{
			cout << 0 << endl;
			return;
		}
		if (temp1 != i)
		{
			for (int j = i; j <= n; ++j) swap(b[i][j], b[temp1][j]);
			ans *= -1;
		}
		for (int i1 = 1; i1 <= n; ++i1)
		{
			for (int j1 = 1; j1 <= n; ++j1) cerr << b[i1][j1] << ' ';
			cerr << endl;
		}
		for (int j = i + 1; j <= n; ++j) if (b[j][i])
		{
			ans = ans * get(b[i][i], P - 2) % P;
			if (ans < 0) ans += P;
			for (k = i + 1; k <= n; ++k)
			{
				b[j][k] = (b[i][k] * (-b[j][i]) + b[j][k] * b[i][i]) % P;
				if (b[j][k] < 0) b[j][k] += P;
			}
			b[j][i] = 0;
		}
		for (int i1 = 1; i1 <= n; ++i1)
		{
			for (int j1 = 1; j1 <= n; ++j1) cerr << b[i1][j1] << ' ';
			cerr << endl;
		}
		ans = ans * b[i][i] % P;
	}
	if (ans < 0) ans += P;
	cout << ans << endl;
}

int main()
{
	scanf("%d%d%d", &N, &M, &P);
	for (int i = 0; i < M; ++i)
	{
		int a, b;
		scanf("%d%d", &a, &b);
		s[++tail] = a; next[tail] = g1[b]; g1[b] = tail;
		++in[a], --ou[b];
	}
	int n(0), closed(1), open(0);
	for (int i = 1; i <= N; ++i) if (!in[i])
	{
		d[++open] = i;
		a[i][open] = 1;
	}
	n = open;
	for (; closed <= open; ++closed)
	{
		int t = d[closed];
		for (int temp = g1[t]; temp; temp = next[temp])
		{
			int q = s[temp];
			for (int j = 1; j <= n; ++j)
			{
				a[q][j] += a[t][j];
				if (a[q][j] >= P) a[q][j] -= P;
			}
			--in[q];
			if (!in[q]) d[++open] = q;
		}
	}
	int s1(0);
	for (int i = 1; i <= N; ++i) if (!ou[i])
	{
		++s1;
		for (int j = 1; j <= n; ++j) b[s1][j] = a[i][j];
	}
	gauss(n);
}


#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>

using namespace std;

const int maxn = 610, maxm = 200010, maxs = 310;
int N, M, P;
int tail(0), g1[maxn], s[maxm], next[maxm], in[maxn], ou[maxn], d[maxn];
int a[maxn][maxs];
int b[maxs][maxs];

int get(int a, int b)
{
    int ans(1), te(a);
    while (b)
    {
        if (b & 1) ans = (long long)ans * te % P;
        te = te * (long long)te % P;
        b >>= 1;
    }
    return ans;
}

void gauss(const int &n)
{
    long long ans = 1;

    for (int i = 1; i <= n; ++i)
    {
        int k(0), temp1(0);
        for (int j = i; j <= n; ++j)
            if (b[j][i] > k)
                k = b[j][i], temp1 = j;
        if (!k)
        {
            cout << 0 << endl;
            return;
        }
        if (temp1 != i)
        {
            for (int j = i; j <= n; ++j) swap(b[i][j], b[temp1][j]);
            ans *= -1;
        }

        for (int j = i + 1; j <= n; ++j) if (b[j][i])
        {
            ans = ans * get(b[i][i], P - 2) % P;
            long long te1 = -b[j][i], te2 = b[i][i];
            for (k = i; k <= n; ++k)
            {
                b[j][k] = (b[i][k] * te1 + b[j][k] * te2) % P;
                if (b[j][k] < 0) b[j][k] += P;
            }
        }

        ans = ans * b[i][i] % P;
    }
    if (ans < 0) ans += P;
    cout << ans << endl;
}

int main()
{
    scanf("%d%d%d", &N, &M, &P);
    for (int i = 0; i < M; ++i)
    {
        int a, b;
        scanf("%d%d", &a, &b);
        s[++tail] = a; next[tail] = g1[b]; g1[b] = tail;
        ++in[a], ++ou[b];
    }
    int n(0), closed(1), open(0);
    for (int i = 1; i <= N; ++i) if (!in[i])
    {
        d[++open] = i;
        a[i][open] = 1;
    }
    n = open;
    for (; closed <= open; ++closed)
    {
        int t = d[closed];
        for (int temp = g1[t]; temp; temp = next[temp])
        {
            int q = s[temp];
            for (int j = 1; j <= n; ++j)
            {
                a[q][j] += a[t][j];
                if (a[q][j] >= P) a[q][j] -= P;
            }
            --in[q];
            if (!in[q]) d[++open] = q;
        }
    }
    if (open > N)
    {
        cout <<"wrong" << endl;
        return 0;
    }
    int s1(0);
    for (int i = 1; i <= N; ++i) if (!ou[i])
    {
        ++s1;
        for (int j = 1; j <= n; ++j) b[s1][j] = a[i][j];
    }
    gauss(n);
}


#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>

using namespace std;

const int maxn = 610, maxm = 200010, maxs = 310;
int N, M, P;
int tail(0), g1[maxn], s[maxm], next[maxm], in[maxn], ou[maxn], d[maxn];
int a[maxn][maxn];
int b[maxn][maxn];

int get(int a, int b)
{
    int ans(1), te(a);
    while (b)
    {
        if (b & 1) ans = (long long)ans * te % P;
        te = te * (long long)te % P;
        b >>= 1;
    }
    return ans;
}

void gauss(const int &n)
{
    long long ans = 1;

    for (int i = 1; i <= n; ++i)
    {
        int k(0), temp1(0);
        for (int j = i; j <= n; ++j)
            if (b[j][i] > k)
                k = b[j][i], temp1 = j;
        if (!k)
        {
            cout << 0 << endl;
            return;
        }
        if (temp1 != i)
        {
            for (int j = i; j <= n; ++j) swap(b[i][j], b[temp1][j]);
            ans *= -1;
        }

        for (int j = i + 1; j <= n; ++j) if (b[j][i])
        {
            ans = ans * get(b[i][i], P - 2) % P;
            long long te1 = -b[j][i], te2 = b[i][i];
            for (k = i; k <= n; ++k)
            {
                b[j][k] = (b[i][k] * te1 + b[j][k] * te2) % P;
                if (b[j][k] < 0) b[j][k] += P;
            }
        }

        ans = ans * b[i][i] % P;
    }
    if (ans < 0) ans += P;
    cout << ans << endl;
}

int main()
{
    scanf("%d%d%d", &N, &M, &P);
    for (int i = 0; i < M; ++i)
    {
        int a, b;
        scanf("%d%d", &a, &b);
        s[++tail] = a; next[tail] = g1[b]; g1[b] = tail;
        ++in[a], ++ou[b];
    }
    int n(0), closed(1), open(0);
    for (int i = 1; i <= N; ++i) if (!in[i])
    {
        d[++open] = i;
        a[i][open] = 1;
    }
    n = open;
    for (; closed <= open; ++closed)
    {
        int t = d[closed];
        for (int temp = g1[t]; temp; temp = next[temp])
        {
            int q = s[temp];
            for (int j = 1; j <= n; ++j)
            {
                a[q][j] += a[t][j];
                if (a[q][j] >= P) a[q][j] -= P;
            }
            --in[q];
            if (!in[q]) d[++open] = q;
        }
    }
    int s1(0);
    for (int i = 1; i <= N; ++i) if (!ou[i])
    {
        ++s1;
        for (int j = 1; j <= n; ++j) b[s1][j] = a[i][j];
    }
    gauss(n);
}


#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>

using namespace std;

const int maxn = 610, maxm = 200010, maxs = 310;
int N, M, P;
int tail(0), g1[maxn], s[maxm], next[maxm], in[maxn], ou[maxn], d[maxn];
int a[maxs][maxs], b[maxs][maxs];

int get(int a, int b)
{
	int ans(1), te(a);
	while (b)
	{
		if (b & 1) ans = (long long)ans * te % P;
		te = te * (long long)te % P;
		b >>= 1;
	}
	return ans;
}

void gauss(const int &n)
{
	long long ans = 1;
	for (int i1 = 1; i1 <= n; ++i1)
		{
			for (int j1 = 1; j1 <= n; ++j1) cerr << b[i1][j1] << ' ';
			cerr << endl;
		}
	for (int i = 1; i <= n; ++i)
	{
		int k(0), temp1(0);
		for (int j = i; j <= n; ++j)
			if (b[j][i] > k)
				k = b[j][i], temp1 = j;
		if (!k)
		{
			cout << 0 << endl;
			return;
		}
		if (temp1 != i)
		{
			for (int j = i; j <= n; ++j) swap(b[i][j], b[temp1][j]);
			ans *= -1;
		}
		for (int i1 = 1; i1 <= n; ++i1)
		{
			for (int j1 = 1; j1 <= n; ++j1) cerr << b[i1][j1] << ' ';
			cerr << endl;
		}
		for (int j = i + 1; j <= n; ++j) if (a[j][i])
		{
			ans = ans * get(b[i][i], P - 2) % P;
			for (k = i + 1; k <= n; ++k)
			{
				b[j][k] = (b[i][k] * (-b[j][i]) + b[j][k] * b[i][i]) % P;
				if (b[j][k] < 0) b[j][k] += P;
			}
			b[j][i] = 0;
		}
		for (int i1 = 1; i1 <= n; ++i1)
		{
			for (int j1 = 1; j1 <= n; ++j1) cerr << b[i1][j1] << ' ';
			cerr << endl;
		}
		ans = ans * b[i][i] % P;
	}
	if (ans < 0) ans += P;
	cout << ans << endl;
}

int main()
{
	scanf("%d%d%d", &N, &M, &P);
	for (int i = 0; i < M; ++i)
	{
		int a, b;
		scanf("%d%d", &a, &b);
		s[++tail] = a; next[tail] = g1[b]; g1[b] = tail;
		++in[a], --ou[b];
	}
	int n(0), closed(1), open(0);
	for (int i = 1; i <= N; ++i) if (!in[i])
	{
		d[++open] = i;
		a[i][open] = 1;
	}
	n = open;
	for (; closed <= open; ++closed)
	{
		int t = d[closed];
		for (int temp = g1[t]; temp; temp = next[temp])
		{
			int q = s[temp];
			for (int j = 1; j <= n; ++j)
			{
				a[q][j] += a[t][j];
				if (a[q][j] >= P) a[q][j] -= P;
			}
			--in[q];
			if (!in[q]) d[++open] = q;
		}
	}
	int s1(0);
	for (int i = 1; i <= N; ++i) if (!ou[i])
	{
		++s1;
		for (int j = 1; j <= n; ++j) b[s1][j] = a[i][j];
	}
	gauss(n);
}


#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>

using namespace std;

const int maxn = 610, maxm = 200010, maxs = 310;
int N, M, P;
int tail(0), g1[maxn], s[maxm], next[maxm], in[maxn], ou[maxn], d[maxn];
int a[maxn][maxs];
long long b[maxs][maxs];

int get(int a, int b)
{
	int ans(1), te(a);
	while (b)
	{
		if (b & 1) ans = (long long)ans * te % P;
		te = te * (long long)te % P;
		b >>= 1;
	}
	return ans;
}

void gauss(const int &n)
{
	long long ans = 1;
	for (int i1 = 1; i1 <= n; ++i1)
		{
			for (int j1 = 1; j1 <= n; ++j1) cerr << b[i1][j1] << ' ';
			cerr << endl;
		}
	for (int i = 1; i <= n; ++i)
	{
		int k(0), temp1(0);
		for (int j = i; j <= n; ++j)
			if (b[j][i] > k)
				k = b[j][i], temp1 = j;
		if (!k)
		{
			cout << 0 << endl;
			return;
		}
		if (temp1 != i)
		{
			for (int j = i; j <= n; ++j) swap(b[i][j], b[temp1][j]);
			ans *= -1;
		}
		for (int i1 = 1; i1 <= n; ++i1)
		{
			for (int j1 = 1; j1 <= n; ++j1) cerr << b[i1][j1] << ' ';
			cerr << endl;
		}
		for (int j = i + 1; j <= n; ++j) if (b[j][i])
		{
			ans = ans * get(b[i][i], P - 2) % P;
			for (k = i + 1; k <= n; ++k)
			{
				b[j][k] = (b[i][k] * (-b[j][i]) + b[j][k] * b[i][i]) % P;
				if (b[j][k] < 0) b[j][k] += P;
			}
			b[j][i] = 0;
		}
		for (int i1 = 1; i1 <= n; ++i1)
		{
			for (int j1 = 1; j1 <= n; ++j1) cerr << b[i1][j1] << ' ';
			cerr << endl;
		}
		ans = ans * b[i][i] % P;
	}
	if (ans < 0) ans += P;
	cout << ans << endl;
}

int main()
{
	scanf("%d%d%d", &N, &M, &P);
	for (int i = 0; i < M; ++i)
	{
		int a, b;
		scanf("%d%d", &a, &b);
		s[++tail] = a; next[tail] = g1[b]; g1[b] = tail;
		++in[a], ++ou[b];
	}
	int n(0), closed(1), open(0);
	for (int i = 1; i <= N; ++i) if (!in[i])
	{
		d[++open] = i;
		a[i][open] = 1;
	}
	n = open;
	for (; closed <= open; ++closed)
	{
		int t = d[closed];
		for (int temp = g1[t]; temp; temp = next[temp])
		{
			int q = s[temp];
			for (int j = 1; j <= n; ++j)
			{
				a[q][j] += a[t][j];
				if (a[q][j] >= P) a[q][j] -= P;
			}
			--in[q];
			if (!in[q]) d[++open] = q;
		}
	}
	int s1(0);
	for (int i = 1; i <= N; ++i) if (!ou[i])
	{
		++s1;
		for (int j = 1; j <= n; ++j) b[s1][j] = a[i][j];
	}
	gauss(n);
}


#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>

using namespace std;

const int maxn = 610, maxm = 200010, maxs = 310;
int N, M, P;
int tail(0), g1[maxn], s[maxm], next[maxm], in[maxn], ou[maxn], d[maxn];
int a[maxn][maxs];
int b[maxs][maxs];

int get(int a, int b)
{
	int ans(1), te(a);
	while (b)
	{
		if (b & 1) ans = (long long)ans * te % P;
		te = te * (long long)te % P;
		b >>= 1;
	}
	return ans;
}

void gauss(const int &n)
{
	long long ans = 1;
	for (int i1 = 1; i1 <= n; ++i1)
		{
			for (int j1 = 1; j1 <= n; ++j1) cerr << b[i1][j1] << ' ';
			cerr << endl;
		}
	for (int i = 1; i <= n; ++i)
	{
		int k(0), temp1(0);
		for (int j = i; j <= n; ++j)
			if (b[j][i] > k)
				k = b[j][i], temp1 = j;
		if (!k)
		{
			cout << 0 << endl;
			return;
		}
		if (temp1 != i)
		{
			for (int j = i; j <= n; ++j) swap(b[i][j], b[temp1][j]);
			ans *= -1;
		}
		for (int i1 = 1; i1 <= n; ++i1)
		{
			for (int j1 = 1; j1 <= n; ++j1) cerr << b[i1][j1] << ' ';
			cerr << endl;
		}
		for (int j = i + 1; j <= n; ++j) if (b[j][i])
		{
			ans = ans * get(b[i][i], P - 2) % P;
			long long te1 = -b[j][i], te2 = b[i][i];
			for (k = i; k <= n; ++k)
			{
				b[j][k] = (b[i][k] * te1 + b[j][k] * te2) % P;
				if (b[j][k] < 0) b[j][k] += P;
			}
			b[j][i] = 0;
		}
		for (int i1 = 1; i1 <= n; ++i1)
		{
			for (int j1 = 1; j1 <= n; ++j1) cerr << b[i1][j1] << ' ';
			cerr << endl;
		}
		ans = ans * b[i][i] % P;
	}
	if (ans < 0) ans += P;
	cout << ans << endl;
}

int main()
{
	scanf("%d%d%d", &N, &M, &P);
	for (int i = 0; i < M; ++i)
	{
		int a, b;
		scanf("%d%d", &a, &b);
		s[++tail] = a; next[tail] = g1[b]; g1[b] = tail;
		++in[a], ++ou[b];
	}
	int n(0), closed(1), open(0);
	for (int i = 1; i <= N; ++i) if (!in[i])
	{
		d[++open] = i;
		a[i][open] = 1;
	}
	n = open;
	for (; closed <= open; ++closed)
	{
		int t = d[closed];
		for (int temp = g1[t]; temp; temp = next[temp])
		{
			int q = s[temp];
			for (int j = 1; j <= n; ++j)
			{
				a[q][j] += a[t][j];
				if (a[q][j] >= P) a[q][j] -= P;
			}
			--in[q];
			if (!in[q]) d[++open] = q;
		}
	}
	int s1(0);
	for (int i = 1; i <= N; ++i) if (!ou[i])
	{
		++s1;
		for (int j = 1; j <= n; ++j) b[s1][j] = a[i][j];
	}
	gauss(n);
}


#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>

using namespace std;

const int maxn = 610, maxm = 200010, maxs = 310;
int N, M, P, tail(0);
int g1[maxn], s[maxm], next[maxm], in[maxn], now[maxn], d[maxn], a[maxs][maxs];

inline void get(const int & a, const int & b)
{
	s[++tail] = b, next[tail] = g1[a], g1[a] = tail;
	++in[b];
}

void read()
{
	scanf("%d%d%d", & N, & M, & P);
	memset(g1, 0, sizeof(g1));
	for (int i = 0; i < M; ++i)
	{
		int a, b;
		scanf("%d%d", & a, & b);
		get(a, b);
	}
}

void prepare()
{
	int open(0), closed(0);
	for (int i = 1; i <= N; ++i) if (!in[i])
	{
		a[i][i] = 1;
		d[++open] = i;
	}
	memset(now, 0, sizeof(now));
	for (; closed < open; ++closed)
	{
		int t = d[closed];
		for (int temp = g1[t]; temp; temp = next[temp])
		{
			for (int j = 1; j <= N; ++j) a[s[temp]][j] = (a[s[temp]][j] + a[t][j]) % P;
			if (++now[s[temp]] == in[s[temp]]) d[++open] = s[temp];
		}
	}
	for (int i = 1; i <= N; ++i)
		for (int j = 1; j <= N; ++j) cerr << "i j: " << i << ' ' << j << " a[i][j]: " << a[i][j] << endl;
}

int power(const int &a, const int &b)
{
	if (b == 1) return a;
	int c = (a * a) % P;
	if (b & 1) c = c * a % P;
	return c;
}

void gauss()
{
	long long ans = 1;
	for (int i = 1; i <= N; ++i)
	{
		int t = i, temp1 = a[i][i];
		for (int j = i + 1; j <= N; ++j) if (a[j][i] > temp1) t = j, temp1 = a[j][i];
		if (!temp1)
		{
			cout << 0 << endl;
			return;
		}
		if (t != i)
			for (int j = i; j <= N; ++j) swap(a[i][j], a[t][j]);
		for (int j = i + 1; j <= N; ++j) if (a[j][i])
		{
			ans = (ans * power(a[i][i], P - 2)) % P;
			for (int k = i; k <= N; ++k)
			{
				a[j][k] = (a[i][k] * (-a[j][i]) + a[j][k] * a[i][i]) % P;
				if (a[j][k] < 0) a[j][k] += P;
			}
		}
	}
	for (int i = 1; i <= N; ++i) ans = ans * a[i][i] % P;
	cout << ans << endl;
}

int main()
{
	read();
	prepare();
	gauss();
}


#include <map>
#include <set>
#include <list>
#include <ctime>
#include <cmath>
#include <queue>
#include <stack>
#include <bitset>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <sstream>
#include <iomanip>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
long long a[605],b[605],p[605],q[605];
long long f[605][605],g[605][605];
vector<long long> c[605];
queue<long long> d;
int main ( ) {
	long long n,m,MOD,i,j;
	scanf("%I64d%I64d%I64d",&n,&m,&MOD);
	for (i=1;i<=m;i++) {
		long long x,y;
		scanf("%I64d%I64d",&x,&y);
		a[y]++;b[x]++;
		c[x].push_back(y);
	}
	for (i=1;i<=n;i++) {
		if (!a[i]) p[++p[0]]=i;
		if (!b[i]) q[++q[0]]=i;
	}
	for (i=1;i<=p[0];i++) {
		g[p[i]][p[i]]=1;
		d.push(p[i]);
	}
	while (d.size()!=0) {
		long long x=d.front(),y;
		d.pop();
		for (i=0;i<c[x].size();i++) {
			if (!--a[y=c[x][i]]) d.push(y);
			for (j=1;j<=p[0];j++) g[p[j]][y]=(g[p[j]][y]+g[p[j]][x])%MOD;
		}
	}
	for (i=1;i<=p[0];i++) {
		for (j=1;j<=q[0];j++) f[i][j]=g[p[i]][q[j]];
	}
	long long ans=1;
	for (i=1;i<=p[0];i++) {
		for (j=i+1;j<=q[0];j++) {
			while (f[j][i]) {
				long long x=f[i][i]/f[j][i],k;
				for (k=i;k<=p[0];k++) f[i][k]=(f[i][k]-f[j][k]*x)%MOD;
				for (k=i;k<=p[0];k++) swap(f[i][k],f[j][k]);
				ans=-ans;
			}
		}
		ans=ans*f[i][i]%MOD;
	}
	printf("%I64d\n",(ans+MOD)%MOD);
	return 0;
}
/*
Sample test(s)
input
4 2 1000003
1 3
2 4
output
1
input
4 2 1000003
4 1
3 2
output
1000002
input
4 4 1000003
2 1
2 4
3 1
3 4
output
0
input
6 5 1000003
1 4
1 5
1 6
2 6
3 6
output
0
input
5 2 1000003
5 1
3 4
output
1
*/


#include <map>
#include <set>
#include <list>
#include <ctime>
#include <cmath>
#include <queue>
#include <stack>
#include <bitset>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <sstream>
#include <iomanip>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
long long a[305][305];
int b[605],c[605],f[605],n,m,p,q;
vector <int> g[605],u,v;
long long mypow (long long x,long long y) {
    if (y==0) return 1;
    long long t=mypow(x,y/2);
    t=t*t%p;
    if (y%2==1) t=t*x%p;
    return t;
}
int dfs (int x) {
    int i,r=0;
    if (f[x]!=-1) return f[x];
    for (i=0;i<g[x].size();i++) {
        r+=dfs(g[x][i]);
        if (r>=p) r-=p;
    }
    f[x]=r;
    return r;
}
long long work ( ) {
    int i,j,k;
    bool h=false;
    long long r;
    for (j=0;j<q;j++) {
        for (i=j;i<q;i++) {
			if (a[i][j]!=0) break;
		}
        if (i==q) return 0;
        if (i!=j) {
            h=!h;
            for (k=0;k<q;k++) swap(a[i][k],a[j][k]);
        }
        for (i=j+1;i<q;i++) {
            long long t=(p-a[i][j])*mypow(a[j][j],p-2)%p;
            for (k=j;k<q;k++) a[i][k]=(a[i][k]+a[j][k]*t)%p;
        }
    }
    if (h==true) r=p-1; else r=1;
    for (i=0;i<q;i++) r=r*a[i][i]%p;
    return r;
}
int main ( ) {
    int x,y,i,j;
    scanf("%d%d%d",&n,&m,&p);
    for (i=0;i<m;i++) {
        scanf("%d%d",&x,&y);
        x--;y--;
        b[y]++;c[x]++;
        g[y].push_back(x);
    }
    for (i=0;i<n;i++) {
        if (b[i]>0 && c[i]==0) v.push_back(i);
        if (b[i]==0 && c[i]>0) u.push_back(i);
    }
    q=v.size();
    for (i=0;i<q;i++) {
        for (j=0;j<n;j++) f[j]=-1;
        f[u[i]]=1;
        for (j=0;j<q;j++) a[i][j]=dfs(v[j]);
    }
    long long r=work();
    bool h=false;
    for (i=0;i<n;i++) {
		if (b[i]==0 && c[i]==0) {
        	int s1=0,s2=0;
        	for (j=0;j<q;j++) {
				if (v[j]<i) s1++;
			}
        	for (j=0;j<q;j++) {
				if (u[j]<i) s2++;
			}
        	if (s1%2!=s2%2) h=!h;
		}
	}
    if (h==true) r=(p-r)%p;
    printf("%I64d\n",r);
    return 0;
}
/*
Sample test(s)

input

4 2 1000003
1 3
2 4

output

1

input

4 2 1000003
4 1
3 2

output

1000002

input

4 4 1000003
2 1
2 4
3 1
3 4

output

0

input

6 5 1000003
1 4
1 5
1 6
2 6
3 6

output

0

input

5 2 1000003
5 1
3 4

output

1
*/


#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;

#define maxn 610
typedef long long ll;
int P;

int N;
vector<int> g[maxn];
int in[maxn],out[maxn];
vector<int> source,sink;
int dp[maxn];

int dfs(int u)
{
    if (dp[u]!=-1) return dp[u];
    int ans=0;
    for(int i=0;i<g[u].size();i++)
    {
        ans+=dfs(g[u][i]);
        if(ans>=P) ans-=P;
    }
    return dp[u]=ans;
}

int D;
ll a[maxn][maxn];

ll pow(ll x,ll n)
{
    if (n==0) return 1;
    ll ans=pow(x,n>>1);
    ans=ans*ans%P;
    if (n&1) ans=ans*x%P;
    return y;
}

ll calc()
{
    bool neg=false;
    for(int j=0;j<D;j++)
    {
        int i;
        for(i=j;i<D;i++)
            if(a[i][j]!=0) break;
        if(i==D) return 0;
        if(i!=j)
        {
            neg=!neg;
            for(int k=0;k<D;k++)
                swap(a[i][k],a[j][k]);
        }
        for(i=j+1;i<D;i++)
        {
            ll coef=(P-a[i][j])*pow(a[j][j],P-2)%P;
            for(int k=j;k<D;k++)
                a[i][k]=(a[i][k]+a[j][k]*coef)%P;
        }
    }
    ll ans=(neg?(P-1):1);
    for(int i=0;i<D;i++)
        ans=ans*a[i][i]%P;
    return ans;
}

int main()
{
    ios::sync_with_stdio(false);
    int M;
    cin>>N>>M>>P;
    for(int i=0;i<M;i++)
    {
        int x,y;
        cin>>x>>y;
        x--;y--;
        in[y]++;out[x]++;
        g[y].push_back(x);
    }

    for(int i=0;i<N;i++)
    {
        if(in[i]>0 && out[i]==0) sink.push_back(i);
        if(in[i]==0 && out[i]>0) source.push_back(i);
    }

    D=sink.size();
    for(int i=0;i<D;i++)
    {
        memset(dp,-1,sizeof(dp));
        dp[source[i]]=1;
        for(int j=0;j<D;j++)
            a[i][j]=dfs(sink[j]);
    }

    ll ans=calc();
    bool neg=false;
    for(int i=0;i<N;i++)
    {
        if(in[i]==0 && out[i]==0)
        {
            int cnt1=0,cnt2=0;
            for(int j=0;j<D;j++)
            {
                if(sink[j]<i) cnt1++;
                if(source[j]<i) cnt2++;
            }
            if((cnt1&1)!=(cnt2&1))
                neg=!neg;
        }
    }
    if(neg) ans=(P-ans)%P;
    cout<<ans<<endl;
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;

#define maxn 610
typedef long long ll;
int P;

int N;
vector<int> g[maxn];
int in[maxn],out[maxn];
vector<int> source,sink;
int dp[maxn];

int dfs(int u)
{
    if (dp[u]!=-1) return dp[u];
    int ans=0;
    for(int i=0;i<g[u].size();i++)
    {
        ans+=dfs(g[u][i]);
        if(ans>=P) ans-=P;
    }
    return dp[u]=ans;
}

int D;
ll a[maxn][maxn];

inline ll pow(ll x,ll n)
{
    if (n==0) return 1;
    ll ans=pow(x,n>>1);
    ans=ans*ans%P;
    if (n&1) ans=ans*x%P;
    return ans;
}

ll calc()
{
    bool neg=false;
    for(int j=0;j<D;j++)
    {
        int i;
        for(i=j;i<D;i++)
            if(a[i][j]!=0) break;
        if(i==D) return 0;
        if(i!=j)
        {
            neg=!neg;
            for(int k=0;k<D;k++)
                swap(a[i][k],a[j][k]);
        }
        for(i=j+1;i<D;i++)
        {
            ll coef=(P-a[i][j])*pow(a[j][j],P-2)%P;
            for(int k=j;k<D;k++)
                a[i][k]=(a[i][k]+a[j][k]*coef)%P;
        }
    }
    ll ans=(neg?(P-1):1);
    for(int i=0;i<D;i++)
        ans=ans*a[i][i]%P;
    return ans;
}

int main()
{
    ios::sync_with_stdio(false);
    int M;
    cin>>N>>M>>P;
    for(int i=0;i<M;i++)
    {
        int x,y;
        cin>>x>>y;
        x--;y--;
        in[y]++;out[x]++;
        g[y].push_back(x);
    }

    for(int i=0;i<N;i++)
    {
        if(in[i]>0 && out[i]==0) sink.push_back(i);
        if(in[i]==0 && out[i]>0) source.push_back(i);
    }

    D=sink.size();
    for(int i=0;i<D;i++)
    {
        memset(dp,-1,sizeof(dp));
        dp[source[i]]=1;
        for(int j=0;j<D;j++)
            a[i][j]=dfs(sink[j]);
    }

    ll ans=calc();
    bool neg=false;
    for(int i=0;i<N;i++)
    {
        if(in[i]==0 && out[i]==0)
        {
            int cnt1=0,cnt2=0;
            for(int j=0;j<D;j++)
            {
                if(sink[j]<i) cnt1++;
                if(source[j]<i) cnt2++;
            }
            if((cnt1&1)!=(cnt2&1))
                neg=!neg;
        }
    }
    if(neg) ans=(P-ans)%P;
    cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define maxn 610
typedef long long ll;
vector<int> g[maxn];
ll f[maxn][maxn];
int ss[maxn],tt[maxn];
int in[maxn],out[maxn];
ll dp[maxn];
int n,m;
ll mod;

ll Pow(ll x,ll y)
{
	if (y==0) return 1;
	if (y==1) return x;
	ll res=Pow(x,y>>1);
	res=res*res%mod;
	if (y&1) res=res*x%mod;
	return res;
}

ll Inv(ll x)
{
	return Pow(x,mod-2);
}

void upd(ll x,ll y)
{
	x+=y;
	x%=mod;
}

int dfs(int v)
{
	if (dp[v]!=-1) return dp[v];
	ll ans=0;
	for(int i=0;i<g[v].size();i++)
		ans=(ans+dfs(g[v][i]))%mod;
	return dp[v]=ans;
}

int main()
{
	ios::sync_with_stdio(false);
	cin>>n>>m>>mod;
	for(int i=1;i<=m;i++)
	{
		int u,v;
		cin>>u>>v;
		out[u]++;in[v]++;
		g[v].push_back(u);
	}
	int sss=0,ttt=0;
	for(int i=1;i<=n;i++)
	{
		if (in[i]==0) ss[++sss]=i;
		if (out[i]==0) tt[++ttt]=i;
	}

	int k=sss;
	for(int i=1;i<=k;i++)
	{
		memset(dp,-1,sizeof(dp));
		dp[ss[i]]=1;
		for(int j=1;j<=k;j++)
			f[i][j]=dfs(tt[j]);
	}
	/*
	for(int i=1;i<=k;i++)
	{
		for(int j=1;j<=k;j++)
		{
			cerr<<f[i][j]<<' ';
		}
		cerr<<endl;
	}
	*/
	ll ans=1;
	for(int i=1;i<=k;i++)
	{
		int p=0;
		for(int j=i;j<=k;j++)
		{
			if (f[j][i])
			{
				p=j;
				break;
			}
		}
		if (p!=i) ans=-ans;
		for(int j=1;j<=k;j++)
			swap(f[p][j],f[i][j]);

		for(int j=i+1;j<=k;j++)
		{
			ll w=mod-f[j][i]*Inv(f[i][i])%mod;
			w=(w%mod+mod)%mod;
			for(int l=i;l<=k;l++)
			{
				f[j][l]+=f[i][l]*w;
				f[j][l]%=mod;
			}
		}
	}

	ans=(ans%mod+mod)%mod;
	for(int i=1;i<=k;i++)
		ans=ans*f[i][i]%mod;

	ans=(ans%mod+mod)%mod;

	cout<<ans<<endl;
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;

#define maxn 610
typedef long long ll;
int P;

int N;
vector<int> g[maxn];
int in[maxn],out[maxn];
vector<int> source,sink;
int dp[maxn];

int dfs(int u)
{
    if (dp[u]!=-1) return dp[u];
    int ans=0;
    for(int i=0;i<g[u].size();i++)
    {
        ans+=dfs(g[u][i]);
        if(ans>=P) ans-=P;
    }
    return dp[u]=ans;
}

int D;
ll a[maxn][maxn];

ll pow(ll x,ll n)
{
    if (n==0) return 1;
    ll ans=pow(x,n>>1);
    ans=ans*ans%P;
    if (n&1) ans=ans*x%P;
    return ans;
}

ll calc()
{
    bool neg=false;
    for(int j=0;j<D;j++)
    {
        int i;
        for(i=j;i<D;i++)
            if(a[i][j]!=0) break;
        if(i==D) return 0;
        if(i!=j)
        {
            neg=!neg;
            for(int k=0;k<D;k++)
                swap(a[i][k],a[j][k]);
        }
        for(i=j+1;i<D;i++)
        {
            ll coef=(P-a[i][j])*pow(a[j][j],P-2)%P;
            for(int k=j;k<D;k++)
                a[i][k]=(a[i][k]+a[j][k]*coef)%P;
        }
    }
    ll ans=(neg?(P-1):1);
    for(int i=0;i<D;i++)
        ans=ans*a[i][i]%P;
    return ans;
}

int main()
{
    ios::sync_with_stdio(false);
    int M;
    cin>>N>>M>>P;
    for(int i=0;i<M;i++)
    {
        int x,y;
        cin>>x>>y;
        x--;y--;
        in[y]++;out[x]++;
        g[y].push_back(x);
    }

    for(int i=0;i<N;i++)
    {
        if(in[i]>0 && out[i]==0) sink.push_back(i);
        if(in[i]==0 && out[i]>0) source.push_back(i);
    }

    D=sink.size();
    for(int i=0;i<D;i++)
    {
        memset(dp,-1,sizeof(dp));
        dp[source[i]]=1;
        for(int j=0;j<D;j++)
            a[i][j]=dfs(sink[j]);
    }

    ll ans=calc();
    bool neg=false;
    for(int i=0;i<N;i++)
    {
        if(in[i]==0 && out[i]==0)
        {
            int cnt1=0,cnt2=0;
            for(int j=0;j<D;j++)
            {
                if(sink[j]<i) cnt1++;
                if(source[j]<i) cnt2++;
            }
            if((cnt1&1)!=(cnt2&1))
                neg=!neg;
        }
    }
    if(neg) ans=(P-ans)%P;
    cout<<ans<<endl;
    return 0;
}


/*
ID:huangta3
PROG:
LANG:C++
*/
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <list>
#include <queue>
#include <vector>
#include <ctime>
#include <set>
#include <bitset>
#include <deque>
#include <fstream>
#include <stack>
#include <map>
#include <utility>
#include <cassert>
#include <string>
#include <iterator>
#include <cctype>
using namespace std;
typedef long long LL;
const int maxn=603,maxm=100003;
int n,m,p,Link[maxn],t[maxm],pre[maxm],in[maxn],out[maxn],S[maxn],T[maxn],s[maxn],tot=0,flag=0;
LL a[maxn][maxn],f[maxn][maxn],ans=1;
int get()
{
    int f=0,v=0;char ch;
    while(!isdigit(ch=getchar()))if(ch=='-')break;
    if(ch=='-')f=1;else v=ch-48;
    while(isdigit(ch=getchar()))v=v*10+ch-48;
    if(f==1)return -v;else return v;
}

LL Pow(LL a,int b)
{
    LL res=1;
    for(;b>0;b/=2)
    {
        if(b%2==1)res=res*a%p;
        a=a*a%p;
    }
    return res;
}

int main()
{
    n=get(),m=get(),p=get();
    for(int i=1;i<=m;i++)
    {
        int x=get(),y=get();
        pre[++tot]=Link[x]; Link[x]=tot; t[tot]=y;
        in[y]++,out[x]++;
    }
    tot=0;
    for(int i=1;i<=n;i++)
        if(in[i]==0)s[tot]=i,S[++tot]=i,f[i][i]=1;
    tot=0;
    for(int i=1;i<=n;i++)
        if(out[i]==0)T[++tot]=i;

    int front=0,rear=tot,N=tot;
    while(front!=rear)
    {
        int x=s[front];
        for(int i=Link[x];i>0;i=pre[i])
        {
            in[t[i]]--;
            if(in[t[i]]==0)s[rear++]=t[i];
            for(int j=1;j<=N;j++)
                (f[S[j]][t[i]]+=f[S[j]][x])%=p;
        }
        front++;
    }

    for(int i=1;i<=N;i++)
        for(int j=1;j<=N;j++)a[i][j]=f[S[i]][T[j]];
    for(int i=1;i<=N;i++)
    {
        int master=0;
        for(int j=i;j<=N;j++)
            if(a[j][i]!=0){master=j;break;}
        if(master==0)return puts("0"),0;
        for(int j=1;j<=N;j++)swap(a[i][j],a[master][j]);
        if(master!=i)flag^=1;
        LL x=Pow(a[i][i],p-2);
        ans=ans*a[i][i]%p;
        for(int j=i+1;j<=N;j++)(a[i][j]*=x)%=p;
        for(int j=i+1;j<=N;j++)
            for(int k=N;k>=i+1;k--)a[j][k]=((a[j][k]-a[i][k]*a[j][i])%p+p)%p;
    }
    if(flag)ans=p-ans;
    printf("%d\n",int(ans));
    return 0;
}


/*
ID:huangta3
PROG:
LANG:C++
*/
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <list>
#include <queue>
#include <vector>
#include <ctime>
#include <set>
#include <bitset>
#include <deque>
#include <fstream>
#include <stack>
#include <map>
#include <utility>
#include <cassert>
#include <string>
#include <iterator>
#include <cctype>
using namespace std;
typedef long long LL;
const int maxn=603,maxm=100003;
int n,m,p,Link[maxn],t[maxm],pre[maxm],in[maxn],out[maxn],S[maxn],T[maxn],s[maxn],tot=0,flag=0;
LL a[maxn][maxn],f[maxn][maxn],ans=1;
int get()
{
    int f=0,v=0;char ch;
    while(!isdigit(ch=getchar()))if(ch=='-')break;
    if(ch=='-')f=1;else v=ch-48;
    while(isdigit(ch=getchar()))v=v*10+ch-48;
    if(f==1)return -v;else return v;
}

LL Pow(LL a,int b)
{
    LL res=1;
    for(;b>0;b/=2)
    {
        if(b%2==1)res=res*a%p;
        a=a*a%p;
    }
    return res;
}

int main()
{
    n=get(),m=get(),p=get();
    for(int i=1;i<=m;i++)
    {
        int x=get(),y=get();
        pre[++tot]=Link[x]; Link[x]=tot; t[tot]=y;
        in[y]++,out[x]++;
    }
    tot=0;
    for(int i=1;i<=n;i++)
        if(in[i]==0)s[tot]=S[++tot]=i,f[i][i]=1;
    tot=0;
    for(int i=1;i<=n;i++)
        if(out[i]==0)T[++tot]=i;

    int front=0,rear=tot,N=tot;
    while(front!=rear)
    {
        int x=s[front];
        for(int i=Link[x];i>0;i=pre[i])
        {
            in[t[i]]--;
            if(in[t[i]]==0)s[rear++]=t[i];
            for(int j=1;j<=N;j++)
                (f[S[j]][t[i]]+=f[S[j]][x])%=p;
        }
        front++;
    }

    for(int i=1;i<=N;i++)
        for(int j=1;j<=N;j++)a[i][j]=f[S[i]][T[j]];
    for(int i=1;i<=N;i++)
    {
        int master=0;
        for(int j=i;j<=N;j++)
            if(a[j][i]!=0){master=j;break;}
        if(master==0)return puts("0"),0;
        for(int j=1;j<=N;j++)swap(a[i][j],a[master][j]);
        if(master!=i)flag^=1;
        LL x=Pow(a[i][i],p-2);
        ans=ans*a[i][i]%p;
        for(int j=i;j<=N;j++)(a[i][j]*=x)%=p;
        for(int j=i+1;j<=N;j++)
            for(int k=N;k>=i+1;k--)a[j][k]=((a[j][k]-a[i][k]*a[j][i])%p+p)%p;
    }
    if(flag)ans=p-ans;
    cout<<ans<<endl;
    return 0;
}


/*
ID:huangta3
PROG:
LANG:C++
*/
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <list>
#include <queue>
#include <vector>
#include <ctime>
#include <set>
#include <bitset>
#include <deque>
#include <fstream>
#include <stack>
#include <map>
#include <utility>
#include <cassert>
#include <string>
#include <iterator>
#include <cctype>
using namespace std;
typedef long long LL;
const int maxn=603,maxm=100003;
int n,m,p,Link[maxn],t[maxm],pre[maxm],in[maxn],out[maxn],S[maxn],T[maxn],s[maxn],tot=0,flag=0;
LL a[maxn][maxn],f[maxn][maxn],ans=1;
int get()
{
    int f=0,v=0;char ch;
    while(!isdigit(ch=getchar()))if(ch=='-')break;
    if(ch=='-')f=1;else v=ch-48;
    while(isdigit(ch=getchar()))v=v*10+ch-48;
    if(f==1)return -v;else return v;
}

LL Pow(LL a,int b)
{
    LL res=1;
    for(;b>0;b/=2)
    {
        if(b%2==1)res=res*a%p;
        a=a*a%p;
    }
    return res;
}

int main()
{
    n=get(),m=get(),p=get();
    for(int i=1;i<=m;i++)
    {
        int x=get(),y=get();
        pre[++tot]=Link[x]; Link[x]=tot; t[tot]=y;
        in[y]++,out[x]++;
    }
    tot=0;
    for(int i=1;i<=n;i++)
        if(in[i]==0)s[tot]=S[++tot]=i,f[i][i]=1;
    tot=0;
    for(int i=1;i<=n;i++)
        if(out[i]==0)T[++tot]=i;

    int front=0,rear=tot,N=tot;
    while(front!=rear)
    {
        int x=s[front];
        for(int i=Link[x];i>0;i=pre[i])
        {
            in[t[i]]--;
            if(in[t[i]]==0)s[rear++]=t[i];
            for(int j=1;j<=N;j++)
                f[S[j]][t[i]]+=f[S[j]][x],f[S[j]][t[i]]%=p;
        }
        front++;
    }

    for(int i=1;i<=N;i++)
        for(int j=1;j<=N;j++)a[i][j]=f[S[i]][T[j]];
    for(int i=1;i<=N;i++)
    {
        int master=0;
        for(int j=i;j<=N;j++)
            if(a[j][i]!=0){master=j;break;}
        if(master==0)return puts("0"),0;
        for(int j=1;j<=N;j++)swap(a[i][j],a[master][j]);
        if(master!=i)flag^=1;
        LL x=Pow(a[i][i],p-2);
        ans=ans*a[i][i]%p;
        for(int j=i+1;j<=N;j++)a[i][j]=a[i][j]*x%p;
        for(int j=i+1;j<=N;j++)
            for(int k=N;k>=i+1;k--)a[j][k]=((a[j][k]-a[i][k]*a[j][i])%p+p)%p;
    }
    if(flag)ans=p-ans;
    printf("%I64d\n",ans);
    return 0;
}


/*
ID:huangta3
PROG:
LANG:C++
*/
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <list>
#include <queue>
#include <vector>
#include <ctime>
#include <set>
#include <bitset>
#include <deque>
#include <fstream>
#include <stack>
#include <map>
#include <utility>
#include <cassert>
#include <string>
#include <iterator>
#include <cctype>
using namespace std;
typedef long long LL;
const int maxn=603,maxm=100003;
int n,m,p,Link[maxn],t[maxm],pre[maxm],in[maxn],out[maxn],S[maxn],T[maxn],s[maxn],tot=0,flag=0;
LL a[maxn][maxn],f[maxn][maxn],ans=1;
int get()
{
    int f=0,v=0;char ch;
    while(!isdigit(ch=getchar()))if(ch=='-')break;
    if(ch=='-')f=1;else v=ch-48;
    while(isdigit(ch=getchar()))v=v*10+ch-48;
    if(f==1)return -v;else return v;
}

LL Pow(LL a,int b)
{
    LL res=1;
    for(;b>0;b/=2)
    {
        if(b%2==1)res=res*a%p;
        a=a*a%p;
    }
    return res;
}

int main()
{
    n=get(),m=get(),p=get();
    for(int i=1;i<=m;i++)
    {
        int x=get(),y=get();
        pre[++tot]=Link[x]; Link[x]=tot; t[tot]=y;
        in[y]++,out[x]++;
    }
    tot=0;
    for(int i=1;i<=n;i++)
        if(in[i]==0)s[tot]=S[++tot]=i,f[i][i]=1;
    tot=0;
    for(int i=1;i<=n;i++)
        if(out[i]==0)T[++tot]=i;

    int front=0,rear=tot,N=tot;
    while(front!=rear)
    {
        int x=s[front];
        for(int i=Link[x];i>0;i=pre[i])
        {
            in[t[i]]--;
            if(in[t[i]]==0)s[rear++]=t[i];
            for(int j=1;j<=N;j++)
                f[S[j]][t[i]]+=f[S[j]][x],f[S[j]][t[i]]%=p;
        }
        front++;
    }

    for(int i=1;i<=N;i++)
        for(int j=1;j<=N;j++)a[i][j]=f[S[i]][T[j]];
    for(int i=1;i<=N;i++)
    {
        int master=0;
        for(int j=i;j<=N;j++)
            if(a[j][i]!=0){master=j;break;}
      //  if(master==0)return puts("0"),0;
        for(int j=1;j<=N;j++)swap(a[i][j],a[master][j]);
        if(master!=i)flag^=1;
        LL x=Pow(a[i][i],p-2);
        ans=ans*a[i][i]%p;
        for(int j=i+1;j<=N;j++)a[i][j]=a[i][j]*x%p;
        for(int j=i+1;j<=N;j++)
            for(int k=N;k>=i+1;k--)a[j][k]=((a[j][k]-a[i][k]*a[j][i])%p+p)%p;
    }
    if(flag)ans=p-ans;
    printf("%I64d\n",ans);
    return 0;
}


/*
ID:huangta3
PROG:
LANG:C++
*/
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <list>
#include <queue>
#include <vector>
#include <ctime>
#include <set>
#include <bitset>
#include <deque>
#include <fstream>
#include <stack>
#include <map>
#include <utility>
#include <cassert>
#include <string>
#include <iterator>
#include <cctype>
using namespace std;
typedef long long LL;
const int maxn=603,maxm=100003;
int n,m,p,Link[maxn],t[maxm],pre[maxm],in[maxn],out[maxn],S[maxn],T[maxn],s[maxn],tot=0,flag=0;
LL a[maxn][maxn],f[maxn][maxn],ans=1;
int get()
{
    int f=0,v=0;char ch;
    while(!isdigit(ch=getchar()))if(ch=='-')break;
    if(ch=='-')f=1;else v=ch-48;
    while(isdigit(ch=getchar()))v=v*10+ch-48;
    if(f==1)return -v;else return v;
}

LL Pow(LL a,int b)
{
    LL res=1;
    for(;b>0;b/=2)
    {
        if(b%2==1)res=res*a%p;
        a=a*a%p;
    }
    return res;
}

int main()
{
    n=get(),m=get(),p=get();
    for(int i=1;i<=m;i++)
    {
        int x=get(),y=get();
        pre[++tot]=Link[x]; Link[x]=tot; t[tot]=y;
        in[y]++,out[x]++;
    }
    tot=0;
    for(int i=1;i<=n;i++)
        if(in[i]==0)s[tot]=S[++tot]=i,f[i][i]=1;
    tot=0;
    for(int i=1;i<=n;i++)
        if(out[i]==0)T[++tot]=i;

    int front=0,rear=tot,N=tot;
    while(front!=rear)
    {
        int x=s[front];
        for(int i=Link[x];i>0;i=pre[i])
        {
            in[t[i]]--;
            if(in[t[i]]==0)s[rear++]=t[i];
            for(int j=1;j<=N;j++)
                (f[S[j]][t[i]]+=f[S[j]][x])%=p;
        }
        front++;
    }

    for(int i=1;i<=N;i++)
        for(int j=1;j<=N;j++)a[i][j]=f[S[i]][T[j]];
    for(int i=1;i<=N;i++)
    {
        int master=0;
        for(int j=i;j<=N;j++)
            if(a[j][i]!=0){master=j;break;}
        if(master==0)return puts("0"),0;
        for(int j=1;j<=N;j++)swap(a[i][j],a[master][j]);
        if(master!=i)flag^=1;
        LL x=Pow(a[i][i],p-2);
        ans=ans*a[i][i]%p;
        for(int j=i;j<=N;j++)(a[i][j]*=x)%=p;
        for(int j=i+1;j<=N;j++)
            for(int k=N;k>=i+1;k--)a[j][k]=((a[j][k]-a[i][k]*a[j][i])%p+p)%p;
    }
    if(flag)ans=p-ans;
    printf("%I64d\n",ans);
    return 0;
}


/*
ID:huangta3
PROG:
LANG:C++
*/
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <list>
#include <queue>
#include <vector>
#include <ctime>
#include <set>
#include <bitset>
#include <deque>
#include <fstream>
#include <stack>
#include <map>
#include <utility>
#include <cassert>
#include <string>
#include <iterator>
#include <cctype>
using namespace std;
typedef long long LL;
const int maxn=603,maxm=100003;
int n,m,p,Link[maxn],t[maxm],pre[maxm],in[maxn],out[maxn],S[maxn],T[maxn],s[maxn],tot=0,flag=0;
LL a[maxn][maxn],f[maxn][maxn],ans=1;
int get()
{
    int f=0,v=0;char ch;
    while(!isdigit(ch=getchar()))if(ch=='-')break;
    if(ch=='-')f=1;else v=ch-48;
    while(isdigit(ch=getchar()))v=v*10+ch-48;
    if(f==1)return -v;else return v;
}

LL Pow(LL a,int b)
{
    LL res=1;
    for(;b>0;b/=2)
    {
        if(b%2==1)res=res*a%p;
        a=a*a%p;
    }
    return res;
}

int main()
{
    n=get(),m=get(),p=get();
    for(int i=1;i<=m;i++)
    {
        int x=get(),y=get();
        pre[++tot]=Link[x]; Link[x]=tot; t[tot]=y;
        in[y]++,out[x]++;
    }
    tot=0;
    for(int i=1;i<=n;i++)
        if(in[i]==0)s[tot]=S[++tot]=i,f[i][i]=1;
    tot=0;
    for(int i=1;i<=n;i++)
        if(out[i]==0)T[++tot]=i;

    int front=0,rear=tot,N=tot;
    while(front!=rear)
    {
        int x=s[front];
        for(int i=Link[x];i>0;i=pre[i])
        {
            in[t[i]]--;
            if(in[t[i]]==0)s[rear++]=t[i];
            for(int j=1;j<=N;j++)
                (f[S[j]][t[i]]+=f[S[j]][x])%=p;
        }
        front++;
    }

    for(int i=1;i<=N;i++)
        for(int j=1;j<=N;j++)a[i][j]=f[S[i]][T[j]];
    for(int i=1;i<=N;i++)
    {
        int master=0;
        for(int j=i;j<=N;j++)
            if(a[j][i]!=0){master=j;break;}
        if(master==0)return puts("0"),0;
        for(int j=1;j<=N;j++)swap(a[i][j],a[master][j]);
        if(master!=i)flag^=1;
        LL x=Pow(a[i][i],p-2);
        ans=ans*a[i][i]%p;
        for(int j=i+1;j<=N;j++)(a[i][j]*=x)%=p;
        for(int j=i+1;j<=N;j++)
            for(int k=N;k>=i+1;k--)a[j][k]=((a[j][k]-a[i][k]*a[j][i])%p+p)%p;
    }
    if(flag)ans=p-ans;
    printf("%I64d\n",ans);
    return 0;
}


#include <cstdio>
#include <iostream>
#include <cstring>
int head[1000],next[100005],od[1000],id[1000],source[1000],sink[1000],from[100005],to[100005],Sk,Se;
long long int f[1000],p; long long int det[1000][1000];
long long int dfs(int u)
{
  long long int re = 0,i;
  if(f[u] > -1)
    return f[u];
  for(i=head[u];i;i=next[i])
    {
      re += dfs(to[i]);
      re %= p;
    }
  f[u] = re;
  return re;
}
long long int power(long long int b,long long int a)
{
  long long int s=1,x=1;
  a = ((a%p)+p)%p;
  for(;x<=a;x*=2)
    {
      if(x & a)
	s = (s*b)%p;
      b = (b*b)%p;
    }
  return s;
}
int solve()
{
  int n = Sk; long long int sign = 1,re =1; int i,j,k;
  for(i=1;i<=n;i++)
    {
      for(j=i;j<=n;j++)
	if(det[j][i] != 0)
	  {
	    if(i != j)
	      {
		for(k=1;k<=n;k++)
		  {long long int tmp=det[i][k]; det[i][k]=det[j][k]; det[j][k]=tmp;}
		sign = -sign;
	      }
	    break;
	  }
      if(j == n+1) return 0;
      for(j=i+1;j<=n;j++)
	if(det[j][i] > 0)
	  {
	    long long int corr = power(det[i][i],p-2);
	    corr = (corr*(p-det[j][i]))%p;
	    for(k=i;k<=n;k++)
	      det[j][k] = (det[j][k]+det[i][k]*corr)%p;
	  }
    }
  for(i=1;i<=n;i++)
    re = (re*det[i][i])%p;
  return ((re*sign)%p+p)%p;
}
int main()
{
  //freopen("e.in","r",stdin);
  //freopen("e.out","w",stdout);
  int n,m,i,k2,k,u,v; long long int sig=1;
  scanf("%d%d",&n,&m);
  std::cin>>p;
  for(i=1;i<=m;i++)
    {
      scanf("%d%d",&from[i],&to[i]);
      next[i] = head[from[i]];
      head[from[i]] = i;
      od[from[i]]++; id[to[i]]++;
    }
  for(i=1;i<=n;i++)
    {
      if(od[i] == 0 && id[i]>0)
	sink[++Sk] = i;
      if(id[i] == 0 && od[i]>0)
	source[++Se] = i;
    }
  for(i=1;i<=n;i++)
    if(od[i] == 0 && id[i] == 0)
      {
	int cnt = 0;
	for(k=1;k<=Sk;k++)
	  if(sink[k] < i) cnt++;
	for(k=1;k<=Se;k++)
	  if(source[k] < i) cnt++;
	if(cnt%2) sig = -sig;
      }
  for(k=1;k<=Se;k++)
    {
      u = source[k];
      for(k2=1;k2<=Sk;k2++)
	{
	  v = sink[k2];
	  memset(f,-1,sizeof(f));
	  f[v] = 1;
	  det[k][k2] = dfs(u);
	}
    }
  long long int sol = solve()*sig;
  sol = (sol%p+p)%p;
  std::cout<<sol;
}


#include <cstdio>
#include <iostream>
#include <cstring>
int head[1000],next[100005],od[1000],id[1000],source[1000],sink[1000],from[100005],to[100005],Sk,Se;
long long int f[1000],p; double det[1000][1000];
int dfs(int u)
{
  int re = 0,i;
  if(f[u] > -1)
    return f[u];
  for(i=head[u];i;i=next[i])
    {
      re += dfs(to[i]);
      re %= p;
    }
  f[u] = re;
  return re;
}
int solve()
{
  int n = Sk,sign = 1,re =1,i,j,k;
  for(i=1;i<=n;i++)
    {
      for(j=i;j<=n;j++)
	if(det[j][i] > 0)
	  {
	    if(i != j)
	      {
		for(k=1;k<=n;k++)
		  {int tmp=det[i][k]; det[i][k]=det[j][k]; det[j][k]=tmp;}
		sign = -sign;
		break;
	      }
	  }
      for(j=i+1;j<=n;j++)
	if(det[j][i] > 0)
	  {
	    double corr = det[j][i]/det[i][i];
	    for(k=i;k<=n;k++)
	      det[j][k] -= det[i][k]*corr;
	  }
    }
  for(i=1;i<=n;i++)
    re *= det[i][i];
  int ret = (int)re;
  if(re-ret>0.5) ret++;
  return ret*sign;
}
int main()
{
  //freopen("e.in","r",stdin);
  //freopen("e.out","w",stdout);
  int n,m,i,k2,k,u,v; int sig=1;
  scanf("%d%d",&n,&m);
  std::cin>>p;
  for(i=1;i<=m;i++)
    {
      scanf("%d%d",&from[i],&to[i]);
      next[i] = head[from[i]];
      head[from[i]] = i;
      od[from[i]]++; id[to[i]]++;
    }
  for(i=1;i<=n;i++)
    {
      if(od[i] == 0 && id[i]>0)
	sink[++Sk] = i;
      if(id[i] == 0 && od[i]>0)
	source[++Se] = i;
    }
  for(i=1;i<=n;i++)
    if(od[i] == 0 && id[i] == 0)
      {
	int cnt = 0;
	for(k=1;k<=Sk;k++)
	  if(sink[k] < i) cnt++;
	for(k=1;k<=Se;k++)
	  if(source[k] < i) cnt++;
	if(cnt%2) sig = -sig;
      }
  for(k=1;k<=Se;k++)
    {
      u = source[k];
      for(k2=1;k2<=Sk;k2++)
	{
	  v = sink[k2];
	  memset(f,-1,sizeof(f));
	  f[v] = 1;
	  det[k][k2] = dfs(u);
	}
    }
  long long int sol = solve()*sig;
  sol = (sol%p+p)%p;
  std::cout<<sol;
}


#include <cstdio>
#include <iostream>
#include <cstring>
int head[1000],next[100005],od[1000],id[1000],source[1000],sink[1000],from[100005],to[100005],Sk,Se;
long long int f[1000],p; long long int det[1000][1000];
int dfs(int u)
{
  int re = 0,i;
  if(f[u] > -1)
    return f[u];
  for(i=head[u];i;i=next[i])
    {
      re += dfs(to[i]);
      re %= p;
    }
  f[u] = re;
  return re;
}
long long int power(long long int b,long long int a)
{
  long long int s=1,x=1;
  a = ((a%p)+p)%p;
  for(;x<=a;x*=2)
    {
      if(x & a)
	s = (s*b)%p;
      b = (b*b)%p;
    }
  return s;
}
int solve()
{
  int n = Sk,sign = 1,re =1,i,j,k;
  for(i=1;i<=n;i++)
    {
      for(j=i;j<=n;j++)
	if(det[j][i] > 0)
	  {
	    if(i != j)
	      {
		for(k=1;k<=n;k++)
		  {int tmp=det[i][k]; det[i][k]=det[j][k]; det[j][k]=tmp;}
		sign = -sign;
		break;
	      }
	  }
      for(j=i+1;j<=n;j++)
	if(det[j][i] > 0)
	  {
	    long long int corr = power(-det[i][i],p-2);
	    corr = (corr*det[j][i])%p;
	    for(k=i;k<=n;k++)
	      det[j][k] = (det[j][k]+det[i][k]*corr)%p;
	  }
    }
  for(i=1;i<=n;i++)
    re = (re*det[i][i])%p;
  return ((re*sign)%p+p)%p;
}
int main()
{
  //freopen("e.in","r",stdin);
  //freopen("e.out","w",stdout);
  int n,m,i,k2,k,u,v; int sig=1;
  scanf("%d%d",&n,&m);
  std::cin>>p;
  for(i=1;i<=m;i++)
    {
      scanf("%d%d",&from[i],&to[i]);
      next[i] = head[from[i]];
      head[from[i]] = i;
      od[from[i]]++; id[to[i]]++;
    }
  for(i=1;i<=n;i++)
    {
      if(od[i] == 0 && id[i]>0)
	sink[++Sk] = i;
      if(id[i] == 0 && od[i]>0)
	source[++Se] = i;
    }
  for(i=1;i<=n;i++)
    if(od[i] == 0 && id[i] == 0)
      {
	int cnt = 0;
	for(k=1;k<=Sk;k++)
	  if(sink[k] < i) cnt++;
	for(k=1;k<=Se;k++)
	  if(source[k] < i) cnt++;
	if(cnt%2) sig = -sig;
      }
  for(k=1;k<=Se;k++)
    {
      u = source[k];
      for(k2=1;k2<=Sk;k2++)
	{
	  v = sink[k2];
	  memset(f,-1,sizeof(f));
	  f[v] = 1;
	  det[k][k2] = dfs(u);
	}
    }
  long long int sol = solve()*sig;
  sol = (sol%p+p)%p;
  std::cout<<sol;
}


#include <cstdio>
#include <iostream>
#include <cstring>
int head[1000],next[100005],od[1000],id[1000],source[1000],sink[1000],from[100005],to[100005],Sk,Se;
long long int f[1000],p; double det[1000][1000];
int dfs(int u)
{
  int re = 0,i;
  if(f[u] > -1)
    return f[u];
  for(i=head[u];i;i=next[i])
    {
      re += dfs(to[i]);
      re %= p;
    }
  f[u] = re;
  return re;
}
int solve()
{
  int n = Sk,sign = 1,re =1,i,j,k;
  for(i=1;i<=n;i++)
    {
      for(j=i;j<=n;j++)
	if(det[j][i] > 0)
	  {
	    if(i != j)
	      {
		for(k=1;k<=n;k++)
		  {int tmp=det[i][k]; det[i][k]=det[j][k]; det[j][k]=tmp;}
		sign = -sign;
		break;
	      }
	  }
      for(j=i+1;j<=n;j++)
	if(det[j][i] > 0)
	  {
	    double corr = det[j][i]/det[i][i];
	    for(k=i;k<=n;k++)
	      det[j][k] -= det[i][k]*corr;
	  }
    }
  for(i=1;i<=n;i++)
    re *= det[i][i];
  return re*sign;
}
int main()
{
  //freopen("e.in","r",stdin);
  //freopen("e.out","w",stdout);
  int n,m,i,k2,k,u,v; int sig=1;
  scanf("%d%d",&n,&m);
  std::cin>>p;
  for(i=1;i<=m;i++)
    {
      scanf("%d%d",&from[i],&to[i]);
      next[i] = head[from[i]];
      head[from[i]] = i;
      od[from[i]]++; id[to[i]]++;
    }
  for(i=1;i<=n;i++)
    {
      if(od[i] == 0 && id[i]>0)
	sink[++Sk] = i;
      if(id[i] == 0 && od[i]>0)
	source[++Se] = i;
    }
  for(i=1;i<=n;i++)
    if(od[i] == 0 && id[i] == 0)
      {
	int cnt = 0;
	for(k=1;k<=Sk;k++)
	  if(sink[k] < i) cnt++;
	for(k=1;k<=Se;k++)
	  if(source[k] < i) cnt++;
	if(cnt%2) sig = -sig;
      }
  for(k=1;k<=Se;k++)
    {
      u = source[k];
      for(k2=1;k2<=Sk;k2++)
	{
	  v = sink[k2];
	  memset(f,-1,sizeof(f));
	  f[v] = 1;
	  det[k][k2] = dfs(u);
	}
    }
  long long int sol = solve()*sig;
  sol = (sol%p+p)%p;
  std::cout<<sol;
}


#include <cstdio>
#include <iostream>
#include <cstring>
int head[1000],next[100005],od[1000],id[1000],source[1000],sink[1000],from[100005],to[100005],Sk,Se;
long long int f[1000],p; long long int det[1000][1000];
long long int dfs(int u)
{
  long long int re = 0,i;
  if(f[u] > -1)
    return f[u];
  for(i=head[u];i;i=next[i])
    {
      re += dfs(to[i]);
      re %= p;
    }
  f[u] = re;
  return re;
}
long long int power(long long int b,long long int a)
{
  long long int s=1,x=1;
  a = ((a%p)+p)%p;
  for(;x<=a;x*=2)
    {
      if(x & a)
	s = (s*b)%p;
      b = (b*b)%p;
    }
  return s;
}
int solve()
{
  int n = Sk; long long int sign = 1,re =1; int i,j,k;
  for(i=1;i<=n;i++)
    {
      for(j=i;j<=n;j++)
	if(det[j][i] != 0)
	  {
	    if(i != j)
	      {
		for(k=1;k<=n;k++)
		  {long long int tmp=det[i][k]; det[i][k]=det[j][k]; det[j][k]=tmp;}
		sign = -sign;
	      }
	    break;
	  }
      if(j == n+1) return 0;
      for(j=i+1;j<=n;j++)
	if(det[j][i] > 0)
	  {
	    long long int corr = power(det[i][i],p-2);
	    corr = (corr*(p-det[j][i]))%p;
	    for(k=i;k<=n;k++)
	      det[j][k] = (det[j][k]+det[i][k]*corr)%p;
	  }
    }
  for(i=1;i<=n;i++)
    re = (re*det[i][i])%p;
  return ((re*sign)%p+p)%p;
}
int main()
{
  //freopen("e.in","r",stdin);
  //freopen("e.out","w",stdout);
  int n,m,i,k2,k,u,v; long long int sig=1;
  scanf("%d%d",&n,&m);
  std::cin>>p;
  for(i=1;i<=m;i++)
    {
      scanf("%d%d",&from[i],&to[i]);
      next[i] = head[from[i]];
      head[from[i]] = i;
      od[from[i]]++; id[to[i]]++;
    }
  for(i=1;i<=n;i++)
    {
      if(od[i] == 0 && id[i]>0)
	sink[++Sk] = i;
      if(id[i] == 0 && od[i]>0)
	source[++Se] = i;
    }
  for(i=1;i<=n;i++)
    if(od[i] == 0 && id[i] == 0)
      {
	int cnt = 0;
	for(k=1;k<=Sk;k++)
	  if(sink[k] < i) cnt++;
	for(k=1;k<=Se;k++)
	  if(source[k] < i) cnt++;
	if(cnt%2) sig = -sig;
      }
  for(k=1;k<=Sk;k++)
    {
      v = sink[k];
      memset(f,-1,sizeof(f));
      f[v] = 1;
      for(k2=1;k2<=Se;k2++)
	{
	  u = source[k2];
	  det[k2][k] = dfs(u);
	}
    }
  long long int sol = solve()*sig;
  sol = (sol%p+p)%p;
  std::cout<<sol;
}


#include <cstdio>
#include <iostream>
#include <cstring>
int head[1000],next[100005],od[1000],id[1000],source[1000],sink[1000],from[100005],to[100005],Sk,Se;
long long int f[1000],p; long long int det[1000][1000];
long long int dfs(int u)
{
  long long int re = 0,i;
  if(f[u] > -1)
    return f[u];
  for(i=head[u];i;i=next[i])
    {
      re += dfs(to[i]);
      re %= p;
    }
  f[u] = re;
  return re;
}
long long int power(long long int b,long long int a)
{
  long long int s=1,x=1;
  a = ((a%p)+p)%p;
  for(;x<=a;x*=2)
    {
      if(x & a)
	s = (s*b)%p;
      b = (b*b)%p;
    }
  return s;
}
int solve()
{
  int n = Sk; long long int sign = 1,re =1; int i,j,k;
  for(i=1;i<=n;i++)
    {
      for(j=i;j<=n;j++)
	if(det[j][i] != 0)
	  {
	    if(i != j)
	      {
		for(k=1;k<=n;k++)
		  {long long int tmp=det[i][k]; det[i][k]=det[j][k]; det[j][k]=tmp;}
		sign = -sign;
		break;
	      }
	  }
      if(j == n+1) return 0;
      for(j=i+1;j<=n;j++)
	if(det[j][i] > 0)
	  {
	    long long int corr = power(det[i][i],p-2);
	    corr = (corr*(p-det[j][i]))%p;
	    for(k=i;k<=n;k++)
	      det[j][k] = (det[j][k]+det[i][k]*corr)%p;
	  }
    }
  for(i=1;i<=n;i++)
    re = (re*det[i][i])%p;
  return ((re*sign)%p+p)%p;
}
int main()
{
  //freopen("e.in","r",stdin);
  //freopen("e.out","w",stdout);
  int n,m,i,k2,k,u,v; long long int sig=1;
  scanf("%d%d",&n,&m);
  std::cin>>p;
  for(i=1;i<=m;i++)
    {
      scanf("%d%d",&from[i],&to[i]);
      next[i] = head[from[i]];
      head[from[i]] = i;
      od[from[i]]++; id[to[i]]++;
    }
  for(i=1;i<=n;i++)
    {
      if(od[i] == 0 && id[i]>0)
	sink[++Sk] = i;
      if(id[i] == 0 && od[i]>0)
	source[++Se] = i;
    }
  for(i=1;i<=n;i++)
    if(od[i] == 0 && id[i] == 0)
      {
	int cnt = 0;
	for(k=1;k<=Sk;k++)
	  if(sink[k] < i) cnt++;
	for(k=1;k<=Se;k++)
	  if(source[k] < i) cnt++;
	if(cnt%2) sig = -sig;
      }
  for(k=1;k<=Se;k++)
    {
      u = source[k];
      for(k2=1;k2<=Sk;k2++)
	{
	  v = sink[k2];
	  memset(f,-1,sizeof(f));
	  f[v] = 1;
	  det[k][k2] = dfs(u);
	}
    }
  long long int sol = solve()*sig;
  sol = (sol%p+p)%p;
  std::cout<<sol;
}


#define FILE_IO 0

#include<algorithm>
#include<cstdio>
#include<cstring>

using std::swap;
typedef long long ll;

const int maxn=610,maxm=200010;
int N,n,m,P;
int fs[maxn],oth[maxm],next[maxm];
int id[maxn],od[maxn];
bool v[maxn];
int d[maxn][maxn],a[maxn][maxn];

void sol(int x){
  if(v[x])return;
  v[x]=1;
  int i,j,t;
  for(i=fs[x];i;i=next[i]){
    sol(t=oth[i]);
    for(j=0;j<n;++j)
      if(d[x][j]+=d[t][j],d[x][j]>P)
        d[x][j]-=P;
  }
}

void pre(){
  int i,x,y;
  scanf("%d%d%d",&N,&m,&P);
  for(i=1;i<=m;++i){
    scanf("%d%d",&x,&y);
    oth[i]=x,next[i]=fs[y],fs[y]=i;
    ++od[x],++id[y];
  }
  for(i=1;i<=N;++i)if(!id[i])
    d[i][n++]=1,v[i]=1;
  for(m=0,i=1;i<=N;++i)if(!od[i])
    sol(i),memcpy(a[m++],d[i],sizeof(d[i]));
}

inline int pow(int x,int y){
  int e=1;
  while(y){
    if(y&1)e=(ll)e*x%P;
    x=(ll)x*x%P;
    y>>=1;
  }
  return e;
}

int gauss(){
  int s=1,i,j,k,t;
  for(i=0;i<n;++i){
    for(j=i;j<n&&!a[j][i];++j);
    if(j==n)return 0;
    if(j!=i)for(s=P-s,k=0;k<n;++k)swap(a[i][k],a[j][k]);
    s=(ll)s*a[i][i]%P,t=pow(a[i][i],P-2);
    for(j=i;j<n;++j)a[i][j]=(ll)a[i][j]*t%P;
    for(j=0;j<n;++j)if(i!=j)
      for(k=n-1;k>=i;--k)
        a[j][k]=(a[j][k]-(ll)a[j][i]*a[i][k]%P+P)%P;
  }
  return s;
}

int main(){
  if(FILE_IO){
    freopen("t.in","r",stdin);
    freopen("t.out","w",stdout);
  }
  pre();
  printf("%d",gauss());
  return 0;
}


#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<iomanip>
#include<queue>
#include<string>
#include<vector>
using namespace std;
#define FF first
#define SS second
#define PB push_back
#define MP make_pair
typedef long long ll;
const ll INF=1<<28;
const ll LINF=1ll<<61;
int n,m,p,d,din[611],dout[611];
ll dp[611][611],a[611][611];
vector<int>con[611],rev[611],s,t;
ll dfs(int x,int y)
{
    if(x==y)return dp[x][y]=1;
    if(dp[x][y]!=-1)return dp[x][y];
    dp[x][y]=0;
    for(int i=0;i<rev[y].size();i++)
    {
        dp[x][y]=(dp[x][y]+dfs(x,rev[y][i]))%p;
    }
    return dp[x][y];
}
ll kissme(ll x,ll k)
{
    int ans=1;
    while(k>0)
    {
        if(k&1)ans=ans*x%p;
        k=k/2;
        x=x*x%p;
    }
    return ans;
}
ll cal()
{
    bool neg=0;
    for(int i=0;i<d;i++)
    {
        int ok=-1;
        for(int j=i;j<d;j++)if(a[j][i]!=0){ok=j;break;}
        if(ok==-1)return 0;
        if(ok!=i)
        {
            for(int j=0;j<d;j++)swap(a[i][j],a[ok][j]);
            neg^=1;
        }
        for(int j=i+1;j<d;j++)
        {
            ll coef=(p-a[j][i])*kissme(a[i][i],p-2)%p;
            for(int k=i;k<d;k++)a[j][k]=(a[j][k]+a[i][k]*coef)%p;
        }
    }
    ll ans=(neg?(p-1):1);
    for(int i=0;i<d;i++)ans=ans*a[i][i]%p;
    return ans;
}
int main()
{
    ios::sync_with_stdio(false);
    cin>>n>>m>>p;int x,y;
    for(int i=1;i<=m;i++)
    {
        cin>>x>>y;
        con[x].PB(y);
        rev[y].PB(x);
        din[y]++;
        dout[x]++;
    }
    for(int i=1;i<=n;i++)
    {
        if(din[i]==0&&dout[i]>0)s.PB(i);
        if(din[i]>0&&dout[i]==0)t.PB(i);
    }d=s.size();
    for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)dp[i][j]=-1;
    for(int i=0;i<d;i++)for(int j=0;j<d;j++)a[i][j]=dfs(s[i],t[j]);
    ll ans=cal();
    bool neg=0;
    for(int i=1;i<=n;i++)if(din[i]==0&&dout[i]==0)
    {
        int cnt1=0,cnt2=0;
        for(int j=0;j<d;j++)if(s[j]<i)cnt1++;
        for(int j=0;j<d;j++)if(t[j]<i)cnt2++;
        if(cnt1%2!=cnt2%2)neg^=1;
    }
    if(neg)ans=(p-ans)%p;
    cout<<ans<<endl;
    return 0;
}


#include <cstdio>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
#define rep(i,a,b) for (int i=a;i<(int)b;i++)
int n,m,p,u,v,id[700],od[700],e[700][700];
vector<int> s,t;queue<int> q;
long long g[700][700],ans=1;
int main() {
	scanf("%d%d%d",&n,&m,&p);
	rep(i,0,m) {scanf("%d%d",&u,&v);id[u]++;od[v]++;e[u][v]++;}
	rep(i,1,n+1) {if(!od[i])s.push_back(i),q.push(i),g[i][i]=1;if(!id[i])t.push_back(i);}
	while (!q.empty()) {
		int u=q.front();
		rep(i,1,n+1) rep(j,0,s.size()) if (e[u][i]) {
			(g[s[j]][i]+=g[s[j]][u]*e[u][i])%=p;
			if (!(od[i]-=e[u][i])) q.push(i);
		}
		q.pop();
	}
	rep(i,0,s.size()) rep (j,0,t.size()) f[i][j]=g[s[i]][t[j]];
	rep(i,0,s.size()) rep(j,i+1,s.size()) while (f[j][i]) {
		int t=f[i][i]/f[j][i];
		rep(k,i,s.size()) (f[i][k]-=f[j][k]*t)%=p;
		rep(k,i,s.size()) swap(f[i][k],f[j][k]);ans=-ans;
	}
	rep(i,0,s.size()) (ans*=f[i][i])%=p;if (ans<0) ans+=p;
	printf("%d\n",(int)ans);
}


#include <cstdio>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
#define rep(i,a,b) for (int i=a;i<(int)b;i++)
int n,m,p,u,v,id[700],od[700],e[700][700];
vector<int> s,t;queue<int> q;
long long g[700][700],f[700][700],ans=1;
int main() {
	scanf("%d%d%d",&n,&m,&p);
	rep(i,0,m) {scanf("%d%d",&u,&v);id[u]++;od[v]++;e[u][v]++;}
	rep(i,1,n+1) {if(!od[i])s.push_back(i),q.push(i),g[i][i]=1;if(!id[i])t.push_back(i);}
	while (!q.empty()) {
		int u=q.front();
		rep(i,1,n+1) rep(j,0,s.size()) if (e[u][i]) {
			(g[s[j]][i]+=g[s[j]][u]*e[u][i])%=p;
			if (!(od[i]-=e[u][i])) q.push(i);
		}
		q.pop();
	}
	rep(i,0,s.size()) rep (j,0,t.size()) f[i][j]=g[s[i]][t[j]];
	rep(i,0,s.size()) rep(j,i+1,s.size()) while (f[j][i]) {
		int t=f[i][i]/f[j][i];
		rep(k,i,s.size()) (f[i][k]-=f[j][k]*t)%=p;
		rep(k,i,s.size()) swap(f[i][k],f[j][k]);ans=-ans;
	}
	rep(i,0,s.size()) (ans*=f[i][i])%=p;if (ans<0) ans+=p;
	printf("%d\n",(int)ans);
}


#include <cstdio>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
#define rep(i,a,b) for (int i=a;i<(int)b;i++)
int n,m,p,u,v,id[700],od[700],e[700][700];
vector<int> s,t;queue<int> q;
long long g[700][700],ans=1;
int main() {
	scanf("%d%d%d",&n,&m,&p);
	rep(i,0,m) {scanf("%d%d",&u,&v);id[u]++;od[v]++;e[u][v]++;}
	rep(i,1,n+1) {if(!od[i])s.push_back(i),q.push(i),g[i][i]=1;if(!id[i])t.push_back(i);}
	while (!q.empty()) {
		int u=q.front();
		rep(i,1,n+1) rep(j,0,s.size()) if (e[u][i]) {
			(g[s[j]][i]+=g[s[j]][u]*e[u][i])%=p;
			if (!(od[i]-=e[u][i])) q.push(i);
		}
		q.pop();
	}
	rep(i,0,s.size()) rep (j,0,t.size()) g[i][j]=g[s[i]][t[j]];
	rep(i,0,s.size()) rep(j,i+1,s.size()) while (g[j][i]) {
		int t=g[i][i]/g[j][i];
		rep(k,i,s.size()) (g[i][k]-=g[j][k]*t)%=p;
		rep(k,i,s.size()) swap(g[i][k],g[j][k]);ans=-ans;
	}
	rep(i,0,s.size()) (ans*=g[i][i])%=p;if (ans<0) ans+=p;
	printf("%d\n",(int)ans);
}


#include <cstdio>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
#define rep(i,a,b) for (int i=a;i<(int)b;i++)
int n,m,p,u,v,id[700],od[700],e[700][700];
vector<int> s,t;queue<int> q;
long long g[700][700],ans=1;
int main() {
	scanf("%d%d%d",&n,&m,&p);
	rep(i,0,m) {scanf("%d%d",&u,&v);id[u]++;od[v]++;e[u][v]++;}
	rep(i,1,n+1) {if(!od[i])s.push_back(i),q.push(i),g[i][i]=1;if(!id[i])t.push_back(i);}
	while (!q.empty()) {
		int u=q.front();
		rep(i,1,n+1) if (e[u][i]) {
			rep(j,0,s.size()) (g[s[j]][i]+=g[s[j]][u]*e[u][i])%=p;
			if (!(od[i]-=e[u][i])) q.push(i);
		}
		q.pop();
	}
	rep(i,0,s.size()) rep (j,0,t.size()) g[i][j]=g[s[i]][t[j]];
	rep(i,0,s.size()) rep(j,i+1,t.size()) while (g[j][i]) {
		int t=g[i][i]/g[j][i];
		rep(k,i,s.size()) (g[i][k]-=g[j][k]*t)%=p;
		rep(k,i,s.size()) swap(g[i][k],g[j][k]);ans=-ans;
	}
	rep(i,0,s.size()) (ans*=g[i][i])%=p;if (ans<0) ans+=p;
	printf("%d\n",(int)ans);
}


#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<string.h>
#include<queue>
#define LL long long
using namespace std;
inline int read( )
{
  int sum=0;char c=getchar( );bool f=0;
  while(c<'0'||c>'9') {if(c=='-') f=1;c=getchar( );}
  while(c>='0'&&c<='9') {sum=sum*10+c-'0';c=getchar( );}
  if(f) return -sum;
  return sum;
}
const int N=605;
int n,m,mod;
struct ex{int num,next;}map[N*N];
int head[N],len,in[N],out[N],IN[N];
inline void link(int x,int y)
{
  len++;map[len].num=y;map[len].next=head[x];
  head[x]=len;in[y]++;out[x]++;
}
int ID[N],f[N],cnt,inc;
LL a[N/2][N/2];queue<int>q;
inline void topo(int S)
{
  int i,x,y;
  q.push(S);f[S]=1;memcpy(in,IN,sizeof(IN));
  while(!q.empty( ))
    {
      x=q.front( );q.pop( );
      if(ID[x]) a[inc][ID[x]]=f[x];
      for(i=head[x];i;i=map[i].next)
	{
	  y=map[i].num;
	  f[y]+=f[x];if(f[y]>=mod) f[y]-=mod;
	  in[y]--;if(!in[y]) q.push(y);
	}
    }
}
inline void exgcd(int &x,int &y,int a,int b)
{
  if(!b) {x=1;y=0;return;}
  exgcd(y,x,b,a%b);y-=(a/b)*x;
}
inline int INV(int a)
{
  int x,y;exgcd(x,y,a,mod);
  return (x%mod+mod)%mod;
}
inline int Guass(int n)
{
  int i,j,k,r,t;bool rev=0;
  for(i=1;i<=n;i++)
    {
      for(j=i;j<=n;j++) if(a[j][i]) break;
      if(j>n) return 0;
      if(i!=j) {for(k=i;k<=n;k++) swap(a[i][k],a[j][k]);rev^=1;}
      r=INV(a[i][i]);
      for(j=i+1;j<=n;j++)
	{
	  t=a[j][i]*r%mod;
	  for(k=i;k<=n;k++) a[j][k]=(a[j][k]-a[i][k]*t%mod+mod)%mod;
	}
    }
  LL ans=1;
  for(i=1;i<=n;i++)
    ans=ans*a[i][i]%mod;
  if(rev) return mod-ans;
  else return ans;
}
int main( )
{
  int i,x,y;
  n=read( );m=read( );mod=read( );
  for(i=1;i<=m;i++) x=read( ),y=read( ),link(x,y);
  memcpy(IN,in,sizeof(in));
  for(i=1;i<=n;i++) if(!out[i]) ID[i]=++cnt;
  for(i=1;i<=n;i++) if(!IN[i]) inc++,topo(i);
  printf("%d",Guass(inc));
  return 0;
}


#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<string.h>
#include<queue>
#define LL long long
using namespace std;
inline int read( )
{
  int sum=0;char c=getchar( );bool f=0;
  while(c<'0'||c>'9') {if(c=='-') f=1;c=getchar( );}
  while(c>='0'&&c<='9') {sum=sum*10+c-'0';c=getchar( );}
  if(f) return -sum;
  return sum;
}
const int N=605;
int n,m,mod;
struct ex{int num,next;}map[N*N];
int head[N],len,in[N],out[N],IN[N];
inline void link(int x,int y)
{
  len++;map[len].num=y;map[len].next=head[x];
  head[x]=len;in[y]++;out[x]++;
}
int ID[N],Q[N],cnt,inc;
LL a[N][N],f[N];queue<int>q;
inline void topo(int S)
{
  int i,j,x,y;
  memcpy(in,IN,sizeof(IN));
  for(i=1;i<S;i++)
    for(j=head[Q[i]];j;j=map[j].next) {in[x=map[j].num]--;if(!in[x]) q.push(x);}
  for(i=S+1;i<=inc;i++)
    for(j=head[Q[i]];j;j=map[j].next) {in[x=map[j].num]--;if(!in[x]) q.push(x);}
  memset(f,0,sizeof(f));
  q.push(Q[S]);f[Q[S]]=1;
  while(!q.empty( ))
    {
      x=q.front( );q.pop( );
      if(ID[x]) a[S][ID[x]]=f[x];
      for(i=head[x];i;i=map[i].next)
	{
	  y=map[i].num;
	  f[y]+=f[x];if(f[y]>=mod) f[y]-=mod;
	  in[y]--;if(!in[y]) q.push(y);
	}
    }
}
inline void exgcd(LL &x,LL &y,LL a,LL b)
{
  if(!b) {x=1;y=0;return;}
  exgcd(y,x,b,a%b);y-=(a/b)*x;
}
inline LL INV(LL a)
{
  LL x,y;exgcd(x,y,a,mod);
  return (x%mod+mod)%mod;
}
inline int Guass(int n)
{
  int i,j,k;LL r,t;bool rev=0;
  for(i=1;i<=n;i++)
    {
      for(j=i;j<=n;j++) if(a[j][i]) break;
      if(j>n) return 0;
      if(i!=j) {for(k=i;k<=n;k++) swap(a[i][k],a[j][k]);rev^=1;}
      r=INV(a[i][i]);
      for(j=i+1;j<=n;j++)
	{
	  t=a[j][i]*r%mod;
	  for(k=i;k<=n;k++) a[j][k]=(a[j][k]-a[i][k]*t%mod+mod)%mod;
	}
    }
  LL ans=1;
  for(i=1;i<=n;i++)
    ans=ans*a[i][i]%mod;
  if(rev) return mod-ans;
  else return ans;
}
int main( )
{
  int i,x,y;
  n=read( );m=read( );mod=read( );
  for(i=1;i<=m;i++) x=read( ),y=read( ),link(x,y);
  memcpy(IN,in,sizeof(in));
  for(i=1;i<=n;i++) if(!out[i]) ID[i]=++cnt;
  for(i=1;i<=n;i++) if(!in[i]) Q[++inc]=i;
  for(i=1;i<=inc;i++) topo(i);
  printf("%d",Guass(inc));
  return 0;
}


#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<string.h>
#include<queue>
#define LL long long
using namespace std;
inline int read( )
{
  int sum=0;char c=getchar( );bool f=0;
  while(c<'0'||c>'9') {if(c=='-') f=1;c=getchar( );}
  while(c>='0'&&c<='9') {sum=sum*10+c-'0';c=getchar( );}
  if(f) return -sum;
  return sum;
}
const int N=605;
int n,m,mod;
struct ex{int num,next;}map[N*N];
int head[N],len,in[N],out[N],IN[N];
inline void link(int x,int y)
{
  len++;map[len].num=y;map[len].next=head[x];
  head[x]=len;in[y]++;out[x]++;
}
int ID[N],Q[N],f[N],cnt,inc;
LL a[N][N];queue<int>q;
inline void topo(int S)
{
  int i,j,x,y;
  memcpy(in,IN,sizeof(IN));
  for(i=1;i<S;i++)
    for(j=head[Q[i]];j;j=map[j].next) {in[x=map[j].num]--;if(!in[x]) q.push(x);}
  for(i=S+1;i<=inc;i++)
    for(j=head[Q[i]];j;j=map[j].next) {in[x=map[j].num]--;if(!in[x]) q.push(x);}
  memset(f,0,sizeof(f));
  q.push(Q[S]);f[Q[S]]=1;
  while(!q.empty( ))
    {
      x=q.front( );q.pop( );
      if(ID[x]) a[S][ID[x]]=f[x];
      for(i=head[x];i;i=map[i].next)
	{
	  y=map[i].num;
	  f[y]+=f[x];if(f[y]>=mod) f[y]-=mod;
	  in[y]--;if(!in[y]) q.push(y);
	}
    }
}
inline void exgcd(int &x,int &y,int a,int b)
{
  if(!b) {x=1;y=0;return;}
  exgcd(y,x,b,a%b);y-=(a/b)*x;
}
inline int INV(int a)
{
  int x,y;exgcd(x,y,a,mod);
  return (x%mod+mod)%mod;
}
inline int Guass(int n)
{
  int i,j,k,r,t;bool rev=0;
  for(i=1;i<=n;i++)
    {
      for(j=i;j<=n;j++) if(a[j][i]) break;
      if(j>n) return 0;
      if(i!=j) {for(k=i;k<=n;k++) swap(a[i][k],a[j][k]);rev^=1;}
      r=INV(a[i][i]);
      for(j=i+1;j<=n;j++)
	{
	  t=a[j][i]*r%mod;
	  for(k=i;k<=n;k++) a[j][k]=(a[j][k]-a[i][k]*t%mod+mod)%mod;
	}
    }
  LL ans=1;
  for(i=1;i<=n;i++)
    ans=ans*a[i][i]%mod;
  if(rev) return mod-ans;
  else return ans;
}
int main( )
{
  int i,x,y;
  n=read( );m=read( );mod=read( );
  for(i=1;i<=m;i++) x=read( ),y=read( ),link(x,y);
  memcpy(IN,in,sizeof(in));
  for(i=1;i<=n;i++) if(!out[i]) ID[i]=++cnt;
  for(i=1;i<=n;i++) if(!IN[i]) Q[++inc]=i;
  for(i=1;i<=inc;i++) topo(i);
  printf("%d",Guass(inc));
  return 0;
}


#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<string.h>
#include<queue>
#define LL long long
using namespace std;
inline int read( )
{
  int sum=0;char c=getchar( );bool f=0;
  while(c<'0'||c>'9') {if(c=='-') f=1;c=getchar( );}
  while(c>='0'&&c<='9') {sum=sum*10+c-'0';c=getchar( );}
  if(f) return -sum;
  return sum;
}
const int N=605;
int n,m,mod;
struct ex{int num,next;}map[N*N];
int head[N],len,in[N],out[N],IN[N];
inline void link(int x,int y)
{
  len++;map[len].num=y;map[len].next=head[x];
  head[x]=len;in[y]++;out[x]++;
}
int ID[N],Q[N],f[N],cnt,inc;
LL a[N/2][N/2];queue<int>q;
inline void topo(int S)
{
  int i,j,x,y;
  memcpy(in,IN,sizeof(IN));
  for(i=1;i<S;i++)
    for(j=head[Q[i]];j;j=map[j].next) {in[x=map[j].num]--;if(!in[x]) q.push(x);}
  for(i=S+1;i<=inc;i++)
    for(j=head[Q[i]];j;j=map[j].next) {in[x=map[j].num]--;if(!in[x]) q.push(x);}
  memset(f,0,sizeof(f));
  q.push(Q[S]);f[Q[S]]=1;
  while(!q.empty( ))
    {
      x=q.front( );q.pop( );
      if(ID[x]) a[S][ID[x]]=f[x];
      for(i=head[x];i;i=map[i].next)
	{
	  y=map[i].num;
	  f[y]+=f[x];if(f[y]>=mod) f[y]-=mod;
	  in[y]--;if(!in[y]) q.push(y);
	}
    }
}
inline void exgcd(int &x,int &y,int a,int b)
{
  if(!b) {x=1;y=0;return;}
  exgcd(y,x,b,a%b);y-=(a/b)*x;
}
inline int INV(int a)
{
  int x,y;exgcd(x,y,a,mod);
  return (x%mod+mod)%mod;
}
inline int Guass(int n)
{
  int i,j,k,r,t;bool rev=0;
  for(i=1;i<=n;i++)
    {
      for(j=i;j<=n;j++) if(a[j][i]) break;
      if(j>n) return 0;
      if(i!=j) {for(k=i;k<=n;k++) swap(a[i][k],a[j][k]);rev^=1;}
      r=INV(a[i][i]);
      for(j=i+1;j<=n;j++)
	{
	  t=a[j][i]*r%mod;
	  for(k=i;k<=n;k++) a[j][k]=(a[j][k]-a[i][k]*t%mod+mod)%mod;
	}
    }
  LL ans=1;
  for(i=1;i<=n;i++)
    ans=ans*a[i][i]%mod;
  if(rev) return mod-ans;
  else return ans;
}
int main( )
{
  int i,x,y;
  n=read( );m=read( );mod=read( );
  for(i=1;i<=m;i++) x=read( ),y=read( ),link(x,y);
  memcpy(IN,in,sizeof(in));
  for(i=1;i<=n;i++) if(!out[i]) ID[i]=++cnt;
  for(i=1;i<=n;i++) if(!in[i]) Q[++inc]=i;
  for(i=1;i<=inc;i++) topo(i);
  printf("%d",Guass(inc));
  return 0;
}


#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<string.h>
#include<queue>
#define LL long long
using namespace std;
inline int read( )
{
  int sum=0;char c=getchar( );bool f=0;
  while(c<'0'||c>'9') {if(c=='-') f=1;c=getchar( );}
  while(c>='0'&&c<='9') {sum=sum*10+c-'0';c=getchar( );}
  if(f) return -sum;
  return sum;
}
const int N=605;
int n,m,mod;
struct ex{int num,next;}map[N*N];
int head[N],len,in[N],out[N],IN[N];
inline void link(int x,int y)
{
  len++;map[len].num=y;map[len].next=head[x];
  head[x]=len;in[y]++;out[x]++;
}
int ID[N],Q[N],cnt,inc;
LL a[N/2][N/2],f[N];queue<int>q;
inline void topo(int S)
{
  int i,j,x,y;
  memcpy(in,IN,sizeof(IN));
  for(i=1;i<S;i++)
    for(j=head[Q[i]];j;j=map[j].next) {in[x=map[j].num]--;if(!in[x]) q.push(x);}
  for(i=S+1;i<=inc;i++)
    for(j=head[Q[i]];j;j=map[j].next) {in[x=map[j].num]--;if(!in[x]) q.push(x);}
  memset(f,0,sizeof(f));
  q.push(Q[S]);f[Q[S]]=1;
  while(!q.empty( ))
    {
      x=q.front( );q.pop( );
      if(ID[x]) a[S][ID[x]]=f[x];
      for(i=head[x];i;i=map[i].next)
	{
	  y=map[i].num;
	  f[y]+=f[x];if(f[y]>=mod) f[y]-=mod;
	  in[y]--;if(!in[y]) q.push(y);
	}
    }
}
inline void exgcd(LL &x,LL &y,LL a,LL b)
{
  if(!b) {x=1;y=0;return;}
  exgcd(y,x,b,a%b);y-=(a/b)*x;
}
inline LL INV(LL a)
{
  LL x,y;exgcd(x,y,a,mod);
  return (x%mod+mod)%mod;
}
inline int Guass(int n)
{
  int i,j,k;LL r,t;bool rev=0;
  for(i=1;i<=n;i++)
    {
      for(j=i;j<=n;j++) if(a[j][i]) break;
      if(j>n) return 0;
      if(i!=j) {for(k=i;k<=n;k++) swap(a[i][k],a[j][k]);rev^=1;}
      r=INV(a[i][i]);
      for(j=i+1;j<=n;j++)
	{
	  t=a[j][i]*r%mod;
	  for(k=i;k<=n;k++) a[j][k]=(a[j][k]-a[i][k]*t%mod+mod)%mod;
	}
    }
  LL ans=1;
  for(i=1;i<=n;i++)
    ans=ans*a[i][i]%mod;
  if(rev) return mod-ans;
  else return ans;
}
int main( )
{
  int i,x,y;
  n=read( );m=read( );mod=read( );
  for(i=1;i<=m;i++) x=read( ),y=read( ),link(x,y);
  memcpy(IN,in,sizeof(in));
  for(i=1;i<=n;i++) if(!out[i]) ID[i]=++cnt;
  for(i=1;i<=n;i++) if(!in[i]) Q[++inc]=i;
  for(i=1;i<=inc;i++) topo(i);
  printf("%d",Guass(inc));
  return 0;
}