#include <bits/stdc++.h>
using namespace std;
inline long long rd() {
  long long x = 0;
  char o, f = 1;
  while (o = getchar(), o < 48)
    if (o == 45) f = -f;
  do x = (x << 3) + (x << 1) + (o ^ 48);
  while (o = getchar(), o > 47);
  return x * f;
}
const long long def = 2e3 + 5;
const long long mod = 1e9 + 7;
struct node {
  long long x, y, z;
} a[def];
long long jc[def], ny[def], sum[def][def];
long long ksm(long long a, long long b, long long mod) {
  long long ans = 1;
  while (b) {
    if (b & 1) ans = (ans * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return ans;
}
long long C(long long n, long long m) {
  if (n < m) return 0;
  return jc[n] * ny[m] % mod * ny[n - m] % mod;
}
long long calc(long long i, long long j) {
  return max(abs(a[i].x - a[j].x), abs(a[i].y - a[j].y));
}
long long get(long long x1, long long y1, long long x2, long long y2) {
  if (x2 < x1 || y2 < y1) return 0;
  x1 = max(x1, 1ll);
  y1 = max(y1, 1ll);
  x2 = min(def - 1, x2);
  y2 = min(def - 1, y2);
  return sum[x2][y2] - sum[x2][y1 - 1] - sum[x1 - 1][y2] + sum[x1 - 1][y1 - 1];
}
int main() {
  long long n, m, r;
  jc[0] = ny[0] = 1;
  for (long long i = 1; i < def; i++) {
    jc[i] = jc[i - 1] * i % mod;
    ny[i] = ksm(jc[i], mod - 2, mod);
  }
  scanf("%lld%lld%lld", &n, &m, &r);
  for (long long i = 1; i <= n; i++) {
    scanf("%lld%lld%lld", &a[i].x, &a[i].y, &a[i].z);
    sum[a[i].x][a[i].y]++;
  }
  for (long long i = 1; i < def; i++)
    for (long long j = 1; j < def; j++)
      sum[i][j] += sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1];
  long long ans = 0;
  for (long long i = 1; i <= n; i++) {
    long long sum = get(a[i].x - r, a[i].y - r, a[i].x + r, a[i].y + r);
    for (long long j = 1; j <= n; j++)
      if (i != j) {
        long long minx = min(a[i].x, a[j].x), miny = min(a[i].y, a[j].y);
        long long maxx = max(a[i].x, a[j].x), maxy = max(a[i].y, a[j].y);
        long long in = get(maxx - r, maxy - r, minx + r, miny + r);
        long long outi =
            get(a[i].x - r, a[i].y - r, a[i].x + r, a[i].y + r) - in;
        long long outj =
            get(a[j].x - r, a[j].y - r, a[j].x + r, a[j].y + r) - in;
        long long all = outi + outj + in;
        long long num = ((C(n, m) - C(n - outi - in, m) - C(n - outj - in, m) +
                          C(n - all, m)) %
                             mod +
                         mod) %
                        mod;
        ans = (ans + num * a[i].z % mod * a[j].z % mod) % mod;
      }
    ans = (ans + ((C(n, m) - C(n - sum, m)) % mod + mod) % mod * a[i].z % mod *
                     a[i].z % mod) %
          mod;
  }
  printf("%lld\n", (ans % mod + mod) % mod);
  return 0;
}
