#include <bits/stdc++.h>
int ij[(200000 + 200000) - 1], ww[(200000 + 200000) - 1];
int ae[(200000 + 200000)][3], mm[(200000 + 200000)], n_, m_;
void link(int i, int j, int w, int h) {
  int tmp;
  if (mm[i] < mm[j]) tmp = i, i = j, j = tmp;
  if (mm[i] >= 3) {
    int i_, j_, h_;
    i_ = n_++;
    h_ = ae[i][--mm[i]], j_ = i ^ ij[h_];
    ij[h_] = i_ ^ j_;
    ae[i_][mm[i_]++] = h_;
    h_ = m_++;
    ij[h_] = i_ ^ i, ww[h_] = 0;
    ae[i][mm[i]++] = h_, ae[i_][mm[i_]++] = h_;
    link(i_, j, w, h);
  } else {
    ij[h] = i ^ j, ww[h] = w;
    ae[i][mm[i]++] = h, ae[j][mm[j]++] = h;
  }
}
int sz[(200000 + 200000)], ae_[(200000 + 200000)][3], mm_[(200000 + 200000)],
    cc[(200000 + 200000)][18];
long long dd[(200000 + 200000)][18];
int kk[(200000 + 200000)];
char used[(200000 + 200000)];
int c_;
void dfs(int p, int i, int n, int l, int c, long long d) {
  int g, centroid;
  if (l >= 0) cc[i][l] = c, dd[i][l] = d, kk[i]++;
  centroid = 1;
  sz[i] = 1;
  for (g = 0; g < mm[i]; g++) {
    int h = ae[i][g], j = i ^ ij[h];
    if (j != p && !used[j]) {
      dfs(i, j, n, l, c, d + ww[h]);
      if (sz[j] * 2 > n) centroid = 0;
      sz[i] += sz[j];
    }
  }
  if ((n - sz[i]) * 2 > n) centroid = 0;
  if (centroid) c_ = i;
}
void cd(int i, int n, int l, int c, int w) {
  int g;
  dfs(-1, i, n, l, c, w);
  if (c != -1) ae_[c][mm_[c]++] = c_;
  used[c = c_] = 1;
  for (g = 0; g < mm[c]; g++) {
    int h;
    h = ae[c][g], i = c ^ ij[h];
    if (!used[i]) cd(i, sz[i] < sz[c] ? sz[i] : n - sz[c], l + 1, c, ww[h]);
  }
}
int tt[1 + ((200000 + 200000) * (18 + 1))][3];
long long sum[1 + ((200000 + 200000) * (18 + 1))];
int cnt[1 + ((200000 + 200000) * (18 + 1))];
int copy(int u, int i) {
  static int v;
  int g;
  v++;
  sum[v] = sum[u], cnt[v] = cnt[u];
  for (g = 0; g < mm_[i]; g++) tt[v][g] = tt[u][g];
  return v;
}
int update(int u, int i) {
  int t, u_, l;
  t = u_ = copy(u, kk[i] == 0 ? i : cc[i][0]);
  for (l = 0; l < kk[i]; l++) {
    int j, j_, g;
    j = cc[i][l], j_ = l + 1 == kk[i] ? i : cc[i][l + 1];
    sum[u_] += dd[i][l], cnt[u_]++;
    for (g = 0; g < mm_[j]; g++)
      if (j_ == ae_[j][g]) {
        u_ = tt[u_][g] = copy(u = tt[u][g], j_);
        break;
      }
    sum[u_] -= dd[i][l];
  }
  cnt[u_]++;
  return t;
}
long long query(int u, int i) {
  int l;
  long long ans;
  ans = 0;
  for (l = 0; l < kk[i]; l++) {
    int j, j_, v, g;
    j = cc[i][l], j_ = l + 1 == kk[i] ? i : cc[i][l + 1];
    v = -1;
    for (g = 0; g < mm_[j]; g++)
      if (j_ == ae_[j][g]) {
        v = tt[u][g];
        break;
      }
    if (u) ans += sum[u] + (cnt[u] - cnt[v]) * dd[i][l];
    u = v;
  }
  ans += sum[u];
  return ans;
}
int main() {
  static int pp[200000], uu[200000];
  int n, q, h, i, j, w;
  long long ans;
  scanf("%d%d", &n, &q);
  for (i = 0; i < n; i++) scanf("%d", &pp[i]), pp[i]--;
  for (i = 0; i < n; i++) ae[i][0] = ae[i][1] = ae[i][2] = -1;
  n_ = n, m_ = n - 1;
  for (h = 0; h < n - 1; h++) {
    scanf("%d%d%d", &i, &j, &w), i--, j--;
    link(i, j, w, h);
  }
  cd(0, n_, -1, -1, 0);
  for (i = 0; i < n; i++) uu[i] = update(i == 0 ? 0 : uu[i - 1], pp[i]);
  ans = 0;
  while (q--) {
    int t;
    scanf("%d", &t);
    if (t == 1) {
      int l, r;
      scanf("%d%d%d", &l, &r, &i), l ^= ans % (1 << 30), r ^= ans % (1 << 30),
          i ^= ans % (1 << 30), l--, r--, i--;
      printf("%lld\n",
             (ans = query(uu[r], i) - (l == 0 ? 0 : query(uu[l - 1], i))));
    } else {
      int tmp;
      scanf("%d", &i), i ^= ans % (1 << 30), i--;
      tmp = pp[i], pp[i] = pp[i + 1], pp[i + 1] = tmp;
      uu[i] = update(i == 0 ? 0 : uu[i - 1], pp[i]);
    }
  }
  return 0;
}
