#include <bits/stdc++.h>
using namespace std;
int n, m, r, i, j, x[2002], y[2002], f[2002][2002];
long long kq, mod = round(1e9) + 7, w[2002], c[2002][2002];
int get(int x1, int y1, int x2, int y2) {
  x1 = max(x1, 1);
  y1 = max(y1, 1);
  x2 = min(x2, 1000);
  y2 = min(y2, 1000);
  if (x1 > x2 || y1 > y2) return 0;
  return f[x2][y2] - f[x2][y1 - 1] - f[x1 - 1][y2] + f[x1 - 1][y1 - 1];
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m >> r;
  for (i = 1; i <= n; i++) {
    cin >> x[i] >> y[i] >> w[i];
    f[x[i]][y[i]]++;
  }
  for (i = 1; i <= 1000; i++)
    for (j = 1; j <= 1000; j++)
      f[i][j] += f[i - 1][j] + f[i][j - 1] - f[i - 1][j - 1];
  for (i = 0; i <= n; i++)
    for (j = i; j <= n; j++)
      if (i == 0)
        c[i][j] = 1;
      else
        c[i][j] = (c[i][j - 1] + c[i - 1][j - 1]) % mod;
  for (i = 1; i <= n; i++)
    for (j = 1; j <= n; j++) {
      int ti = get(x[i] - r, y[i] - r, x[i] + r, y[i] + r),
          tj = get(x[j] - r, y[j] - r, x[j] + r, y[j] + r);
      int tg = ti + tj -
               get(max(x[i], x[j]) - r, max(y[i], y[j]) - r,
                   min(x[i], x[j]) + r, min(y[i], y[j]) + r);
      long long sum = c[m][n];
      sum = (sum - c[m][n - ti] + mod) % mod;
      sum = (sum - c[m][n - tj] + mod) % mod;
      sum = (sum + c[m][n - tg]) % mod;
      kq = (kq + w[i] * w[j] % mod * sum % mod) % mod;
    }
  cout << kq;
}
