#include <bits/stdc++.h>
using namespace std;
const int maxa = 1000;
const long long tt = 1e9 + 7;
int n, m, r, a[2005], b[2005], sum[2005][2005];
long long ans = 0, w[2005], C[2005];
long long exgcd(long long a, long long b, long long &x, long long &y) {
  if (!b) {
    x = 1;
    y = 0;
    return a;
  }
  long long z, res = exgcd(b, a % b, x, y);
  z = y;
  y = x - a / b * y;
  x = z;
  return res;
}
long long inv(long long a, long long b) {
  long long x, y;
  exgcd(a, b, x, y);
  return (x % b + b) % b;
}
bool check(int x, int y) {
  return abs(a[x] - a[y]) <= r && abs(b[x] - b[y]) <= r;
}
int count(int X1, int Y1, int X2, int Y2) {
  if (X1 > X2 || Y1 > Y2) return 0;
  return sum[X2][Y2] - sum[X2][Y1 - 1] - sum[X1 - 1][Y2] + sum[X1 - 1][Y1 - 1];
}
int main() {
  int i, j, x, y, z, X1, X2, Y1, Y2, X3, Y3;
  scanf("%d%d%d", &n, &m, &r);
  memset(sum, 0, sizeof(sum));
  for (i = 1; i <= n; i++)
    scanf("%d%d%lld", &a[i], &b[i], &w[i]), sum[a[i]][b[i]]++;
  for (i = 1; i <= maxa; i++)
    for (j = 1; j <= maxa; j++)
      sum[i][j] += sum[i][j - 1] + sum[i - 1][j] - sum[i - 1][j - 1];
  for (i = 0; i < m; i++) C[i] = 0;
  C[m] = 1;
  for (i = m + 1; i <= n; i++) C[i] = C[i - 1] * i % tt * inv(i - m, tt) % tt;
  for (i = 1; i <= n; i++) {
    X1 = max(a[i] - r, 1);
    Y1 = max(b[i] - r, 1);
    X2 = min(a[i] + r, maxa);
    Y2 = min(b[i] + r, maxa);
    x = sum[X2][Y2] - sum[X2][Y1 - 1] - sum[X1 - 1][Y2] + sum[X1 - 1][Y1 - 1];
    (ans += (C[n] - C[n - x] + tt) * w[i] % tt * w[i] % tt) %= tt;
  }
  for (i = 1; i < n; i++) {
    X1 = max(a[i] - r, 1);
    Y1 = max(b[i] - r, 1);
    X2 = min(a[i] + r, maxa);
    Y2 = min(b[i] + r, maxa);
    x = count(X1, Y1, X2, Y2);
    for (j = i + 1; j <= n; j++) {
      X1 = max(a[j] - r, 1);
      Y1 = max(b[j] - r, 1);
      X2 = min(a[j] + r, maxa);
      Y2 = min(b[j] + r, maxa);
      y = count(X1, Y1, X2, Y2);
      X1 = max(max(a[i], a[j]) - r, 1);
      Y1 = max(max(b[i], b[j]) - r, 1);
      X2 = min(min(a[i], a[j]) + r, maxa);
      Y2 = min(min(b[i], b[j]) + r, maxa);
      z = count(X1, Y1, X2, Y2);
      (ans += (C[n] - C[n - x] - C[n - y] + C[n - x - y + z] + tt * 2) * 2 %
              tt * w[i] % tt * w[j] % tt) %= tt;
    }
  }
  printf("%lld\n", ans);
}
