#include <bits/stdc++.h>
using namespace std;
const int NOI = (1 << 18) + 1;
char buf[NOI], *p1, *p2;
inline int read() {
  int x = 0, f = 1;
  char c =
      ((p1 == p2) && (p2 = (p1 = buf) + fread(buf, 1, NOI, stdin), p1 == p2)
           ? EOF
           : *p1++);
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = ((p1 == p2) && (p2 = (p1 = buf) + fread(buf, 1, NOI, stdin), p1 == p2)
             ? EOF
             : *p1++);
  }
  while (c >= '0' && c <= '9')
    x = (x << 3) + (x << 1) + (c ^ '0'),
    c = ((p1 == p2) && (p2 = (p1 = buf) + fread(buf, 1, NOI, stdin), p1 == p2)
             ? EOF
             : *p1++);
  return x * f;
}
const int N = 1e5 + 5;
int n, m, mod;
long long Pow(long long x, int k) {
  if (k < 0) return 1;
  long long res = 1;
  while (k) {
    if (k & 1) res = res * x % mod;
    x = x * x % mod;
    k >>= 1;
  }
  return res;
}
int f[N], siz[N], k;
inline int get(int x) { return x == f[x] ? x : f[x] = get(f[x]); }
int main() {
  n = read(), m = read(), mod = read();
  for (int i = 1; i <= n; i++) f[i] = i, siz[i] = 1;
  for (int i = 1; i <= m; i++) {
    int x = get(read()), y = get(read());
    if (x == y) continue;
    f[y] = x, siz[x] += siz[y];
  }
  long long res = 1;
  for (int i = 1; i <= n; i++)
    if (f[i] == i) {
      res = res * siz[i] % mod;
      k++;
    }
  if (k == 1) return printf("%d\n", 1 % mod), 0;
  res = res * Pow(n, k - 2) % mod;
  printf("%lld\n", res);
  return 0;
}
