Example 7 Let $M$ be a point on the plane with coordinates $(p \times 1994, 7p \times 1994)$, where $p$ is a prime number. Find the number of right triangles that satisfy the following conditions:
(1) The vertices of the triangle are all integer points, and $M$ is the right-angle vertex;
(2) The incenter of the triangle is the origin.
(1994 CMO Problem)
To simplify the calculation, we translate the origin to \( M \) and establish a new Cartesian coordinate system \( x^{\prime} M y^{\prime} \). In this new system, the coordinates of \( M \) are \( (0,0) \), and the coordinates of the incenter \( O \) are \( (-p \times 1994, -7 p \times 1994) \). Therefore,
\[
|O M| = \sqrt{(p \times 1994)^{2} + (7 p \times 1994)^{2}} = p \times 1994 \times 5 \sqrt{2}.
\]

Let \( \triangle M A B \) be any right triangle that satisfies conditions (1) and (2), with the inradius \( r \). Draw perpendiculars from \( O \) to the legs \( M A \) and \( M B \), with the feet of the perpendiculars being \( D \) and \( C \) respectively (Figure 1-14). Let the slope of \( A M \) be \( k \). It is easy to see that the slope of \( O M \) is
\[
k^{\prime} = \frac{7 p \times 1994}{p \times 1994} = 7.
\]

Noting that \( O \) is the incenter of \( \triangle M A B \) and \( \angle O M A = 45^{\circ} \), we use the formula for the angle between two lines:
\[
1 = \tan 45^{\circ} = \frac{k^{\prime} - k}{1 + k^{\prime} k} = \frac{7 - k}{1 + 7 k},
\]
so \( k = \frac{3}{4} \). Since \( M A \perp M B \), the slope of \( M B \) is \( k_{1} = -\frac{4}{3} \). Therefore, we can set the coordinates of \( A \) and \( B \) as
\[
A(-4 t, -3 t), B\left(3 t^{\prime}, -4 t^{\prime}\right).
\]

Since \( (3,4) = 1 \) and \( A, B \) are integer points, \( t \) and \( t^{\prime} \) are both positive integers. Thus, \( M A = 5 t \) and \( M B = 5 t^{\prime} \), meaning the lengths of the two legs are multiples of 5. Therefore, \( M C = M D = r = M O \cos 45^{\circ} = p \times 1994 \times 5 \).

Let \( B C = u \) and \( A D = v \). Then \( u = M B - M C = 5(t^{\prime} - p \times 1994) \) and \( v = M A - M D = 5(t - p \times 1994) \), so \( u \) and \( v \) are both multiples of 5.

Let \( \angle O B C = \alpha \) and \( \angle O A D = \beta \). Since \( O \) is the incenter of \( \triangle M A B \), we have \( \alpha + \beta = 45^{\circ} \). Thus, \( \tan \alpha = \frac{r}{u} \) and \( \tan \beta = \frac{r}{v} \), and
\[
\frac{r}{u} = \tan \alpha = \tan (45^{\circ} - \beta) = \frac{1 - \tan \beta}{1 + \tan \beta} = \frac{v - r}{v + r},
\]
so \( u = \frac{r(v + r)}{v - r} \). Let \( v - r = m \) and \( \frac{2 r^{2}}{m} = n \), then \( u = \frac{r(m + 2 r)}{m} = r + n \).
Thus, \( m \) and \( n \) are both multiples of 5, and
\[
m n = 2 r^{2} = 2 \times (p \times 1994 \times 5)^{2} = 2^{3} \times 5^{2} \times 997^{2} \times p^{2}.
\]

For each pair of positive integer solutions \( (m, n) \), we can uniquely determine a pair of positive integer solutions \( (u, v) = (n + r, m + r) \), which in turn uniquely determines a right triangle \( \triangle M A B \) that meets the conditions. Conversely, this is also true. Therefore, the number of right triangles that meet the conditions \( S \) is equal to the number of positive integer solutions \( (m, n) \) of the indeterminate equation (1) that satisfy \( 51 m \) and \( 51 n \).

(1) If \( p \neq 2 \) and \( p \neq 997 \), then the solutions of \( \frac{m}{5} \cdot \frac{n}{5} = 2^{3} \times p^{2} \times 997^{2} \) are
\[
\left\{\begin{array}{l}
\frac{m}{5} = 2^{i} \times p^{j} \times 997^{k} \\
\frac{n}{5} = 2^{3-i} \times p^{2-j} \times 997^{2-k}
\end{array}\left(\begin{array}{l}
i=0,1,2,3 \\
j=0,1,2 \\
k=0,1,2
\end{array}\right)\right.
\]
There are \( 4 \times 3 \times 3 = 36 \) solutions.
(2) If \( p = 997 \), then the solutions of \( \frac{m}{5} \cdot \frac{n}{5} = 2^{3} \times 997^{4} \) are
\[
\left\{\begin{array}{l}
\frac{m}{5} = 2^{i} \times 997^{j} \\
\frac{n}{5} = 2^{3-i} \times 997^{4-j}
\end{array}\binom{i=0,1,2,3}{j=0,1,2,3,4}\right.
\]
There are \( 4 \times 5 = 20 \) solutions.
(3) If \( p = 2 \), then the solutions of \( \frac{m}{5} \cdot \frac{n}{5} = 2^{5} \times 1997^{2} \) are
\[
\left\{\begin{array}{l}
\frac{m}{5} = p^{i} \times 1997^{j} \\
\frac{n}{5} = p^{5-i} \times 1997^{2-j}
\end{array} \quad\binom{i=0,1,2,3,4,5}{j=0,1,2}\right.
\]
There are \( 6 \times 3 = 18 \) solutions.
In summary, the number of right triangles that meet the conditions is
\[
S = \left\{\begin{array}{l}
36 \text{ (when } p \neq 2 \text{ and } p \neq 997 \text{)}, \\
20 \text{ (when } p = 997 \text{)}, \\
18 \text{ (when } p = 2 \text{)}.
\end{array}\right.
\]