#include <bits/stdc++.h>
using namespace std;
typedef long long int LL;
int n, m, mod;
const int N = 1e5 + 10, M = 2e6 + 10;
int h[N], hu[N], e[M], ne[M], idx;
int dnf[N], low[N], timestamp;
int stk[N], top;
bool in_stk[N];
int id[N], scc_cnt;
int psize[N];
unordered_set<LL> me;
int dp[N], cnt[N];
void add(int h[], int a, int b)
{
e[idx] = b, ne[idx] = h[a], h[a] = idx ++;
}
void tarjin(int u)
{
dnf[u] = low[u] = ++ timestamp;
stk[++ top] = u, in_stk[u] = true;
for (int i = h[u]; ~i; i = ne[i])
{
int j = e[i];
if (!dnf[j])
{
tarjin(j);
low[u] = min(low[u], low[j]);
}
else if (in_stk[j])
low[u] = min(low[u], dnf[j]);
}
if (low[u] == dnf[u])
{
++ scc_cnt;
int y;
do{
y = stk[top --];
in_stk[y] = false;
id[y] = scc_cnt;
psize[scc_cnt] ++;
} while (y != u);
}
}
int main(void)
{
cin >> n >> m >> mod;
memset(h, -1, sizeof h);
memset(hu, -1, sizeof hu);
for (int i = 0; i < m; i ++)
{
int a, b;
cin >> a >> b;
add(h, a, b);
}
for (int i = 1; i <= n; i ++)
if (!dnf[i])
tarjin(i);
for (int i = 1; i <= n; i ++)
for (int j = h[i]; ~j; j = ne[j])
{
int k = e[j];
int a = id[i], b = id[k];
LL hash = a * 1000000LL + b;
if (a != b && !me.count(hash))
{
me.insert(hash);
add(hu, a, b);
}
}
for (int i = scc_cnt; i >= 1; i --)
{
if (!dp[i]) dp[i] = psize[i], cnt[i] = 1;
for (int j =hu[i]; ~j; j = ne[j])
{
int k = e[j];
if (dp[k] < dp[i] + psize[k])
{
dp[k] = dp[i] + psize[k];
cnt[k] = cnt[i];
}
else if (dp[k] == dp[i] + psize[k]) cnt[k] = (cnt[k] + cnt[i]) % mod;
}
}
int maxv = -1, c = 0;
for (int i = scc_cnt; i>=1; i --)
{
if (maxv < dp[i])
{
maxv = dp[i];
c = cnt[i];
}
else if (maxv == dp[i])
c = (cnt[i] + c) % mod;
}
printf("%d\n%d\n", maxv, c);
return 0;
}