#include <cmath>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <fstream>
#include <iostream>
#define rep(i, l, r) for(int i = l; i <= r; i++)
#define down(i, l, r) for(int i = l; i >= r; i--)
#define MS 123456
#define MAX 1037471823
#define Q 51061
struct node{int y, n;} e[MS*2]; int ec, f[MS];
int n, m, l[MS], r[MS], h[MS], s[MS], ph[MS], x, y, z;
unsigned int k[MS], a[MS], c[MS], sum[MS];
bool rev[MS];
char q[10];
using namespace std;
inline void Cal(int x) { if (!x) return; s[x] = s[l[x]] + s[r[x]] + 1, sum[x] = (((sum[l[x]]+sum[r[x]]+k[x])%Q*c[x])%Q+(a[x]*(s[x]%Q))%Q)%Q; }
inline void Down(int x)
{
if (rev[x]) { int k; k = l[x], l[x] = r[x], r[x] = k, rev[x]^=1, rev[l[x]]^=1, rev[r[x]]^=1; }
k[x] = (k[x]*c[x]+a[x])%Q; c[l[x]] = (c[l[x]]*c[x])%Q; c[r[x]] = (c[r[x]]*c[x])%Q; a[l[x]] = (a[l[x]]*c[x]+a[x])%Q; a[r[x]] = (a[r[x]]*c[x]+a[x])%Q;
a[x] = 0, c[x] = 1;
Cal(l[x]); Cal(r[x]); Cal(x);
}
inline void Splay(int x)
{
if (!x) return; int o = h[x], rr = s[l[x]];
while (o) { if (r[o] == x) rr += s[l[o]]+1; x = o, o = h[x]; }
while (rr != s[l[x]]) { if (rr < s[l[x]]) o = l[x]; else rr -= s[l[x]]+1, o = r[x]; Down(x); x = o; } Down(x);
o = h[x]; while (o)
{
if (!h[o]) ph[x] = ph[o], ph[o] = 0;
if (l[o] == x) { l[h[o]] == o ? l[h[o]] = x : r[h[o]] = x; h[x] = h[o]; l[o] = r[x]; h[r[x]] = o; h[o] = x; r[x] = o; Cal(o); }
else { l[h[o]] == o ? l[h[o]] = x : r[h[o]] = x; h[x] = h[o]; r[o] = l[x]; h[l[x]] = o; h[o] = x; l[x] = o; Cal(o); }
o = h[x];
}
Cal(x);
}
void Search(int x)
{
int o = f[x], y = e[o].y, m = 0, my = 0; s[x] = 1;
while (y) { if (y != h[x]) { h[y] = x; Search(y); if (m < s[y]) m = s[y], my = y; h[y] = 0; } o = e[o].n, y = e[o].y; }
r[x] = my; h[my] = x; Cal(x);
o = f[x], y = e[o].y;
while (y) { if (y != h[x] && y != my) ph[y] = x; o = e[o].n, y = e[o].y; }
}
inline void Acc(int x)
{
int o = 0;
while (x) { Splay(x); ph[r[x]] = x, h[r[x]] = 0, r[x] = o; if (o) h[o] = x, ph[o] = 0; Cal(x); o = x; x = ph[x]; }
}
inline void Evert(int x) { Acc(x); Splay(x); rev[x]^=1; }
inline void Cut(int x, int y) { Evert(x); Acc(y); Splay(y); h[x] = l[y] = 0; Cal(y); }
inline void Link(int x, int y) { Evert(x); Splay(y); ph[r[y]] = y, h[r[y]] = 0, r[y] = x, h[x] = y; Cal(y);}
inline void Add(int x, int y, int z) { Evert(x); Acc(y); Splay(y); a[y] = (a[y] + z) % Q; Cal(y); }
inline void Mult(int x, int y, int z) { Evert(x); Acc(y); Splay(y); c[y] = (c[y] * z) % Q; a[y] *= z; a[y] %= Q; Cal(y); }
inline void QSum(int x, int y) { Evert(x); Acc(y); Splay(y); printf("%d\n", sum[y]%Q); }
int main()
{
scanf("%d%d", &n, &m);
rep(i, 1, n) s[i] = k[i] = c[i] = sum[i] = 1;
rep(i, 1, n-1)
{
scanf("%d%d", &x, &y);
ec++, e[ec].y = y, e[ec].n = f[x], f[x] = ec;
ec++, e[ec].y = x, e[ec].n = f[y], f[y] = ec;
}
Search(1);
rep(i, 1, m)
{
scanf("%s", q);
if (q[0] == '+') { scanf("%d%d%d", &x, &y, &z); Add(x, y, z); }
else if (q[0] == '-') { scanf("%d%d", &x, &y); Cut(x, y); scanf("%d%d", &x, &y); Link(x, y); }
else if (q[0] == '*') { scanf("%d%d%d", &x, &y, &z); Mult(x, y, z); }
else { scanf("%d%d", &x, &y); QSum(x, y); }
}
return 0;
}
I2luY2x1ZGUgPGNtYXRoPgojaW5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