#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
const long long M = 1000000007;
using namespace std;
long long C[2005][2005];
void Combi(long long n, long long m) {
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= i; j++) {
      if (i < j)
        C[i][j] = 0;
      else if (i == j)
        C[i][j] = 1;
      else if (j == 0)
        C[i][j] = 1;
      else {
        C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % M;
      }
    }
  }
}
long long choose(int n, int m) { return C[n][m] % M; }
int n, m, r;
bitset<2000> adj[2000];
long long calc(int i) {
  int c = adj[i].count();
  int nc = n - c;
  long long tmp1 = choose(n, m);
  long long tmp2 = choose(nc, m);
  return (tmp1 - tmp2) % M;
}
long long calc(int i, int j) {
  int both = (adj[i] & adj[j]).count();
  int bi = adj[i].count() - both;
  int bj = adj[j].count() - both;
  long long ans = 0;
  {
    int c = both, nc = n - c;
    long long tmp1 = choose(n, m);
    long long tmp2 = choose(nc, m);
    ans = (tmp1 - tmp2) % M;
  }
  {
    long long tmp = 0;
    tmp = (tmp + choose(n - both - bj, m)) % M;
    tmp = (tmp + choose(n - both - bi, m)) % M;
    tmp = (tmp - choose(n - both - bi - bj, m)) % M;
    long long tmp1 = choose(n - both, m);
    long long tmp2 = tmp;
    ans = (ans + (tmp1 - tmp2)) % M;
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL);
  ;
  int testcase;
  memset(C, 0, sizeof(C));
  Combi(2000, 2000);
  cin >> n >> m >> r;
  vector<long long> x(n), y(n), b(n);
  for (int i = 0; i < n; i++) {
    cin >> x[i] >> y[i] >> b[i];
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (abs(x[i] - x[j]) <= r && abs(y[i] - y[j]) <= r) {
        adj[i][j] = 1;
      }
    }
  }
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      long long val = (b[i] * b[j]) % M;
      if (i == j) {
        ans = (ans + val * calc(i)) % M;
      } else {
        ans = (ans + val * calc(i, j)) % M;
      }
    }
  }
  if (ans < 0) ans += M;
  cout << ans << '\n';
}
