#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int maxn = 3e3 + 555;
struct node {
  long long x, y, v;
  bool operator<(const node& n) const {
    if (x != n.x)
      return x < n.x;
    else
      return y < n.y;
  }
};
long long pre[maxn][maxn];
node ori[maxn];
inline long long cal(long long hang1, long long lie1, long long hang2,
                     long long lie2) {
  hang1 = min(hang1, 1000ll);
  lie1 = min(lie1, 1000ll);
  hang2 = max(hang2, 1ll);
  lie2 = max(lie2, 1ll);
  return pre[hang1][lie1] - pre[hang1][lie2 - 1] - pre[hang2 - 1][lie1] +
         pre[hang2 - 1][lie2 - 1];
}
long long buxuan[maxn];
long long f[maxn];
void init() {
  f[0] = 1;
  for (long long i = 1; i < maxn; i++) f[i] = f[i - 1] * i % mod;
}
inline long long qpow(long long a, long long n) {
  long long base = a;
  long long res = 1;
  while (n) {
    if (n & 1) res = res * base % mod;
    base = base * base % mod;
    n >>= 1;
  }
  return res;
}
long long C(long long n, long long m) {
  if (n < m) return 0;
  return f[n] * qpow(f[m], mod - 2) % mod * qpow(f[n - m], mod - 2) % mod;
}
void solve() {
  long long n, m, r;
  scanf("%lld %lld %lld", &n, &m, &r);
  for (long long i = 1; i <= n; i++) {
    scanf("%lld %lld %lld", &ori[i].x, &ori[i].y, &ori[i].v);
    pre[ori[i].x][ori[i].y] = 1;
  }
  sort(ori + 1, ori + n + 1);
  long long ans = 0;
  for (long long i = 1; i <= 1000; i++)
    for (long long j = 1; j <= 1000; j++) pre[i][j] += pre[i][j - 1];
  for (long long j = 1; j <= 1000; j++)
    for (long long i = 1; i <= 1000; i++) pre[i][j] += pre[i - 1][j];
  long long base = C(n, m);
  for (long long i = 1; i <= n; i++) {
    long long wai =
        n - cal(ori[i].x + r, ori[i].y + r, ori[i].x - r, ori[i].y - r);
    long long cnt = C(wai, m);
    buxuan[i] = cnt;
    ans += ori[i].v * ori[i].v % mod * (base - cnt) % mod;
    ans %= mod;
  }
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j < i; j++) {
      long long wai =
          n - cal(ori[i].x + r, ori[i].y + r, ori[i].x - r, ori[i].y - r);
      wai -= cal(ori[j].x + r, ori[j].y + r, ori[j].x - r, ori[j].y - r);
      if (ori[i].x - ori[j].x <= r * 2) {
        if (abs(ori[j].y - ori[i].y) <= r * 2) {
          if (ori[j].y < ori[i].y)
            wai = wai +
                  cal(ori[j].x + r, ori[j].y + r, ori[i].x - r, ori[i].y - r) %
                      mod;
          else
            wai = wai +
                  cal(ori[j].x + r, ori[i].y + r, ori[i].x - r, ori[j].y - r) %
                      mod;
        }
      }
      long long cnt = (base - buxuan[i] - buxuan[j] + C(wai, m)) % mod;
      cnt += mod;
      cnt %= mod;
      ans += ori[i].v * ori[j].v % mod * 2 % mod * cnt % mod;
      ans %= mod;
    }
  }
  printf("%lld\n", ans);
}
signed main() {
  init();
  solve();
}
