#include <bits/stdc++.h>
using namespace std;
struct Combo {
  long long* fact;
  long long* inv;
  void build(long long n) {
    fact = new long long[n];
    inv = new long long[n];
    fact[0] = 1;
    inv[0] = 1;
    for (int i = 1; i < n; i++) {
      fact[i] = (fact[i - 1] * (long long)i) % 1000000007ll;
      inv[i] = binexp(fact[i], 1000000007ll - 2);
    }
  }
  long long binexp(long long a, long long b) {
    if (b == 0) return 1ll;
    if (b == 1) return a % 1000000007ll;
    if (b % 2) return (a * binexp(a, b - 1)) % 1000000007ll;
    return binexp((a * a) % 1000000007ll, b / 2);
  }
  long long choose(long long a, long long b) {
    if (a < b) return 0;
    long long res = (fact[a] * inv[b]) % 1000000007ll;
    res *= inv[a - b];
    res %= 1000000007ll;
    return res;
  }
} helper;
long long n, m, r;
long long row[2020], col[2020], score[2020];
long long grid[2020][2020];
long long snakes(long long r1, long long c1, long long r2, long long c2) {
  if (r1 > r2 || c1 > c2) return 0;
  return (grid[r2][c2] - grid[r1 - 1][c2] - grid[r2][c1 - 1] +
          grid[r1 - 1][c1 - 1]);
}
int main() {
  helper.build(100000);
  memset(grid, 0, sizeof(grid));
  scanf(" %lld%lld%lld", &n, &m, &r);
  for (int i = 0; i < n; i++) {
    scanf(" %lld%lld%lld", &row[i], &col[i], &score[i]);
    grid[row[i]][col[i]] = 1;
  }
  for (int i = 1; i < 2020; i++) {
    for (int j = 1; j < 2020; j++) grid[i][j] += grid[i][j - 1];
    for (int j = 0; j < 2020; j++) {
      grid[i][j] += grid[i - 1][j];
    }
  }
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      long long r11 = max(1ll, row[i] - r);
      long long c11 = max(1ll, col[i] - r);
      long long r12 = min(1000ll, row[i] + r);
      long long c12 = min(1000ll, col[i] + r);
      long long r21 = max(1ll, row[j] - r);
      long long c21 = max(1ll, col[j] - r);
      long long r22 = min(1000ll, row[j] + r);
      long long c22 = min(1000ll, col[j] + r);
      long long inone = snakes(r11, c11, r12, c12);
      long long intwo = snakes(r21, c21, r22, c22);
      long long inboth =
          inone + intwo -
          snakes(max(r11, r21), max(c11, c21), min(r12, r22), min(c12, c22));
      long long cans = helper.choose(n, m);
      cans -= helper.choose(n - inone, m);
      cans -= helper.choose(n - intwo, m);
      cans += helper.choose(n - inboth, m);
      cans += 1000000007ll * 69;
      cans %= 1000000007ll;
      long long cs = (score[i] * score[j]) % 1000000007ll;
      ans += (cans * cs) % 1000000007ll;
      ans %= 1000000007ll;
    }
  }
  printf("%lld\n", ans);
}
