#P10636. Collinear Triples in a Grid

    ID: 12662 Type: Default 1000ms 256MiB

Collinear Triples in a Grid

Collinear Triples in a Grid

Given an n × m grid of points (a lattice), count the number of unordered triples of distinct points \( (a,b,c) \) that are collinear. Two triples are considered the same if they contain the same points regardless of order. Since the answer can be very large, output it modulo \(10^9+7\).

The grid is arranged in such a way that the point coordinates are \( (x, y) \) with \(0 \le x \lt n\) and \(0 \le y \lt m\). For example, the following is a \( 3 \times 4 \) grid:

3x4 Grid

A triple of points is collinear if they all lie on the same straight line. For a line that contains \( L \) points, the number of triples on that line is given by the binomial coefficient \[ C(L,3)= \frac{L(L-1)(L-2)}{6}. \]

The task is to consider every line that can be drawn on the grid (in any direction) which contains at least three points, and sum \( C(L,3) \) over all such lines.

inputFormat

The first and only line of input contains two integers \( n \) and \( m \) (representing the number of rows and columns respectively).

outputFormat

Output a single integer – the number of collinear triples modulo \(10^9+7\).

sample

3 4
20