# 树链剖分

#include<iostream>
#include<cstdio>
using namespace std;
#define ci const int&
#define ri register int
#define rc register char
#define ls rt<<1
#define rs rt<<1|1
#define N 100100
struct edge{
int nxt,to;
} E[N<<1];
struct node{
int w,l,r,siz,lz;
} T[N<<2];
int n,m,r,p,x,y,z,op;
int w[N],Tw[N];
int cnt,rt;
ri res=0,bf=1;
rc c=getchar();
for(;c<'0' || c>'9';c=getchar())
if(c=='-')
bf=-1;
for(;c>='0' && c<='9';c=getchar())
res=(res<<3)+(res<<1)+(c^'0');
return bf*res;
}
void write(ci x){
if(x>9)
write(x/10);
putchar(x%10^'0');
}
E[cnt].to=v;
}
inline void pushup(ci rt){
(T[rt].w=T[ls].w+T[rs].w)%=p;
}
inline void pushdown(ci rt){
(T[ls].lz+=T[rt].lz)%=p;
(T[rs].lz+=T[rt].lz)%=p;
(T[ls].w+=T[rt].lz*T[ls].siz)%=p;
(T[rs].w+=T[rt].lz*T[rs].siz)%=p;
T[rt].lz=0;
}
void build(ci rt,ci l,ci r){
T[rt].l=l,T[rt].r=r;
T[rt].siz=r-l+1;
if(l==r){
T[rt].w=Tw[l];
return;
}
ri mid=(l+r)>>1;
build(ls,l,mid);
build(rs,mid+1,r);
pushup(rt);
}
void update(ci rt,ci l,ci r,ci k){
if(T[rt].l>=l && T[rt].r<=r){
(T[rt].w+=T[rt].siz*k)%=p;
(T[rt].lz+=k)%=p;
return;
}
if(T[rt].lz)
pushdown(rt);
ri mid=(T[rt].l+T[rt].r)>>1;
if(l<=mid)
update(ls,l,r,k);
if(r>mid)
update(rs,l,r,k);
pushup(rt);
}
int query(ci rt,ci l,ci r){
if(T[rt].l>=l && T[rt].r<=r)
return T[rt].w;
if(T[rt].lz)
pushdown(rt);
ri res=0;
ri mid=(T[rt].l+T[rt].r)>>1;
if(l<=mid)
res+=query(ls,l,r);
if(r>mid)
res+=query(rs,l,r);
return res%p;
}
void dfs1(ci u,ci f,ci d){
siz[u]=1;
dep[u]=d;
fa[u]=f;
ri v=E[i].to;
if(v==fa[u])
continue;
dfs1(v,u,d+1);
siz[u]+=siz[v];
if(siz[v]>siz[son[u]])
son[u]=v;
}
}
void dfs2(ci u,ci t){
top[u]=t;
id[u]=++cnt;
Tw[cnt]=w[u];
if(!son[u])
return;
dfs2(son[u],t);
ri v=E[i].to;
if(v==son[u] || v==fa[u])
continue;
dfs2(v,v);
}
}
inline void Tadd(int x,int y,int k){
for(;top[x]!=top[y];){
if(dep[top[x]]<dep[top[y]])
swap(x,y);
update(1,id[top[x]],id[x],k);
x=fa[top[x]];
}
if(dep[x]>dep[y])
swap(x,y);
update(1,id[x],id[y],k);
}
inline int Tsum(int x,int y){
ri res=0;
for(;top[x]!=top[y];){
if(dep[top[x]]<dep[top[y]])
swap(x,y);
res+=query(1,id[top[x]],id[x])%p;
x=fa[top[x]];
}
if(dep[x]>dep[y])
swap(x,y);
res+=query(1,id[x],id[y]);
return res%p;
}
int main(){
for(ri i=1;i<=n;i++)
for(ri i=1;i<n;i++){
}
cnt=0;
dfs1(r,0,1);
dfs2(r,r);
build(1,1,n);
for(ri i=1;i<=m;i++){
if(op==1){
}
else if(op==2){
write(Tsum(x,y));
puts("");
}
else if(op==3){
}