#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
long long n, m, r, s[2005][2005], x[2005], y[2005], b[2005], C[2005][2005], ans;
long long getsum(long long x0, long long y0, long long wtf,
                 long long ccflovesmoney) {
  x0 = max(1ll, x0);
  y0 = max(1ll, y0);
  wtf = min(wtf, 1000ll);
  ccflovesmoney = min(ccflovesmoney, 1000ll);
  if (x0 > wtf || y0 > ccflovesmoney) return 0;
  return s[wtf][ccflovesmoney] - s[wtf][y0 - 1] - s[x0 - 1][ccflovesmoney] +
         s[x0 - 1][y0 - 1];
}
long long getsum(long long a, long long b) {
  return getsum(a - r, b - r, a + r, b + r);
}
signed main() {
  scanf("%lld%lld%lld", &n, &m, &r);
  for (long long i = 1; i <= n; i++) {
    C[i][0] = C[i][i] = 1;
    for (long long j = 1; j <= i - 1; j++)
      C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % mod;
  }
  for (long long i = 1; i <= n; i++)
    scanf("%lld%lld%lld", &x[i], &y[i], &b[i]), s[x[i]][y[i]]++;
  for (long long i = 1; i <= 1000; i++)
    for (long long j = 1; j <= 1000; j++)
      s[i][j] += s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1];
  for (long long i = 1; i <= n; i++) {
    for (long long j = i; j <= n; j++) {
      long long wtf = x[i], ccflovesmoney = y[i], yyy = x[j], jfkefw = y[j];
      long long w = getsum(max(wtf, yyy) - r, max(ccflovesmoney, jfkefw) - r,
                           min(wtf, yyy) + r, min(ccflovesmoney, jfkefw) + r);
      long long cnt = (C[n][m] - C[n - w][m] + mod) % mod;
      long long u = getsum(wtf, ccflovesmoney), v = getsum(yyy, jfkefw);
      u -= w;
      v -= w;
      long long nn = n - w;
      cnt = (cnt + C[nn][m] - C[nn - u][m] - C[nn - v][m] + C[nn - u - v][m]) %
            mod;
      while (cnt < 0) cnt = (cnt + mod) % mod;
      if (i == j)
        ans = (ans + (1ll * cnt * b[i]) % mod * b[j]) % mod;
      else
        ans = (ans + (2ll * cnt * b[i]) % mod * b[j]) % mod;
    }
  }
  printf("%lld\n", ans);
  return 0;
}
