#include <bits/stdc++.h>
int n, m, mo;
int sz[111111], ace[111111];
int ancestor(int x) { return x - ace[x] ? ace[x] = ancestor(ace[x]) : x; }
int mul(int x, int y) { return (int)((long long int)x * y % mo); }
int main() {
  scanf("%d%d%d", &n, &m, &mo);
  if (mo == 1) {
    puts("0");
    return 0;
  }
  for (int i = 1; i <= n; i++) ace[i] = i;
  while (m--) {
    int x, y;
    scanf("%d%d", &x, &y);
    ace[ancestor(x)] = ancestor(y);
  }
  for (int i = 1; i <= n; i++) sz[ancestor(i)]++;
  int res = 1, w = 0;
  for (int i = 1; i <= n; i++)
    if (sz[i]) {
      res = mul(res, sz[i]);
      w++;
    }
  if (w == 1)
    puts("1");
  else {
    for (w -= 2; w; w--) res = mul(res, n);
    printf("%d\n", res);
  }
  return 0;
}
