n,m,*t=list(map(int,open(0).read().split()))

e=[[]for _ in range(n)]

for a,b in zip(*[iter(t)]*2):

  e[a-1]+=b-1,

  e[b-1]+=a-1,

o=[]

s=[0]

f=[1]*n

for v in s:

  o+=v,

  f[v]=0

  t=[]

  for w in e[v]:

    if f[w]:

      s+=w,

      t+=w,

  e[v]=t

dp1=[0]*n

c=[[]for _ in range(n)]

for v in o[::-1]:

  c1,c2=[1],[1]

  for w in e[v]:

    c1+=c1[-1]*(dp1[w]+1)%m,

  for w in e[v][::-1]:

    c2+=c2[-1]*(dp1[w]+1)%m,

  dp1[v]=c1[-1]

  c[v]=c1,c2[-2::-1]

dp2=[0]*n

dp2[0]=1

for v in o:

  t=dp2[v]

  for w,c1,c2 in zip(e[v],*c[v]):

    dp2[w]=(t*c1*c2+1)%m

for dp1,dp2 in zip(dp1,dp2):

  print((dp1*dp2%m))
n,m,*t=list(map(int,open(0).read().split()))

e=[[]for _ in range(n)]

for a,b in zip(*[iter(t)]*2):

  e[a-1]+=b-1,

  e[b-1]+=a-1,

o=[]

s=[0]

f=[1]*n

for v in s:

  o+=v,

  f[v]=0

  t=[]

  for w in e[v]:

    if f[w]:

      s+=w,

      t+=w,

  e[v]=t

dp1=[0]*n

c=[[]for _ in range(n)]

for v in o[::-1]:

  x,y=[1],[1]

  for w in e[v]:x+=x[-1]*(dp1[w]+1)%m,

  for w in e[v][::-1]:y+=y[-1]*(dp1[w]+1)%m,

  dp1[v]=x[-1]

  c[v]=x,y[-2::-1]

dp2=[1]+[0]*n

for v in o:

  for w,x,y in zip(e[v],*c[v]):dp2[w]=(dp2[v]*x*y+1)%m

for x,y in zip(dp1,dp2):print((x*y%m))
n, m, *t = list(map(int, open(0).read().split()))
e = [[] for _ in range(n)]
for a, b in zip(*[iter(t)] * 2):
    e[a - 1] += (b - 1,)
    e[b - 1] += (a - 1,)
o = []
s = [0]
f = [1] * n
for v in s:
    o += (v,)
    f[v] = 0
    t = []
    for w in e[v]:
        if f[w]:
            s += (w,)
            t += (w,)
    e[v] = t
dp1 = [0] * n
c = [[] for _ in range(n)]
for v in o[::-1]:
    c1, c2 = [1], [1]
    for w in e[v]:
        c1 += (c1[-1] * (dp1[w] + 1) % m,)
    for w in e[v][::-1]:
        c2 += (c2[-1] * (dp1[w] + 1) % m,)
    dp1[v] = c1[-1]
    c[v] = c1, c2[-2::-1]
dp2 = [0] * n
dp2[0] = 1
for v in o:
    t = dp2[v]
    for w, c1, c2 in zip(e[v], *c[v]):
        dp2[w] = (t * c1 * c2 + 1) % m
for dp1, dp2 in zip(dp1, dp2):
    print((dp1 * dp2 % m))

n, m, *t = list(map(int, open(0).read().split()))
e = [[] for _ in range(n)]
for a, b in zip(*[iter(t)] * 2):
    e[a - 1] += (b - 1,)
    e[b - 1] += (a - 1,)
o = []
s = [0]
f = [1] * n
for v in s:
    o += (v,)
    f[v] = 0
    t = []
    for w in e[v]:
        if f[w]:
            s += (w,)
            t += (w,)
    e[v] = t
dp1 = [0] * n
c = [[] for _ in range(n)]
for v in o[::-1]:
    x, y = [1], [1]
    for w in e[v]:
        x += (x[-1] * (dp1[w] + 1) % m,)
    for w in e[v][::-1]:
        y += (y[-1] * (dp1[w] + 1) % m,)
    dp1[v] = x[-1]
    c[v] = x, y[-2::-1]
dp2 = [1] + [0] * n
for v in o:
    for w, x, y in zip(e[v], *c[v]):
        dp2[w] = (dp2[v] * x * y + 1) % m
for x, y in zip(dp1, dp2):
    print((x * y % m))

-    c1, c2 = [1], [1] +    x, y = [1], [1] -        c1 += (c1[-1] * (dp1[w] + 1) % m,) +        x += (x[-1] * (dp1[w] + 1) % m,) -        c2 += (c2[-1] * (dp1[w] + 1) % m,) -    dp1[v] = c1[-1] -    c[v] = c1, c2[-2::-1] -dp2 = [0] * n -dp2[0] = 1 +        y += (y[-1] * (dp1[w] + 1) % m,) +    dp1[v] = x[-1] +    c[v] = x, y[-2::-1] +dp2 = [1] + [0] * n -    t = dp2[v] -    for w, c1, c2 in zip(e[v], *c[v]): -        dp2[w] = (t * c1 * c2 + 1) % m -for dp1, dp2 in zip(dp1, dp2): -    print((dp1 * dp2 % m)) +    for w, x, y in zip(e[v], *c[v]): +        dp2[w] = (dp2[v] * x * y + 1) % m +for x, y in zip(dp1, dp2): +    print((x * y % m))