Let $n$ be a positive integer. There are $n$ ants walking along a line at constant nonzero speeds. Different ants need not walk at the same speed or walk in the same direction. Whenever two or more ants collide, all the ants involved in this collision instantly change directions. (Different ants need not be moving in opposite directions when they collide, since a faster ant may catch up with a slower one that is moving in the same direction.) The ants keep walking indefinitely.
Assuming that the total number of collisions is finite, determine the largest possible number of collisions in terms of $n$.
1. **Understanding the Problem:**
   We have \( n \) ants walking along a line at constant nonzero speeds. They can walk in either direction and change direction upon collision. We need to determine the maximum number of collisions that can occur, assuming the total number of collisions is finite.

2. **Key Insight:**
   The order of the ants along the line does not change. Denote by \( v_1, v_2, \ldots, v_n \) the respective speeds of ants \( 1, 2, \ldots, n \) in this order. If \( v_{i-1} < v_i > v_{i+1} \) for some \( i \in \{2, \ldots, n-1\} \), then ant \( i \) can catch up with ants \( i-1 \) or \( i+1 \) irrespective of their directions of motion, leading to an infinite number of collisions. Hence, for the number of collisions to be finite, the speeds must be ordered such that:
   - \( v_1 \geq v_2 \geq \cdots \geq v_n \) (all ants are moving in the same direction with non-increasing speeds), or
   - \( v_1 \geq \cdots \geq v_{k-1} > v_k \leq \cdots \leq v_n \) for some \( k \in \{2, \ldots, n-1\} \).

3. **Claim:**
   If \( v_1 \geq \cdots \geq v_m \), then ants \( m-1 \) and \( m \) collide at most \( m-1 \) times.

4. **Proof of the Claim:**
   - **Base Case:** For \( m = 1 \), the statement is trivial as there are no collisions.
   - **Inductive Step:** Assume the statement holds for \( m-1 \). Consider \( m \) ants with \( v_1 \geq \cdots \geq v_m \). Ants \( m-1 \) and \( m \) can only collide if \( v_{m-1} \geq v_m \). Between successive collisions with ant \( m \), ant \( m-1 \) must reverse direction by colliding with ant \( m-2 \). By the inductive hypothesis, ants \( m-1 \) and \( m-2 \) collide at most \( m-2 \) times. Therefore, ants \( m \) and \( m-1 \) collide at most \( (m-2) + 1 = m-1 \) times. \(\blacksquare\)

5. **Case Analysis:**
   - **Case (i):** If \( v_1 \geq \cdots \geq v_n \), the total number of collisions is at most:
     \[
     0 + 1 + 2 + \cdots + (n-1) = \frac{n(n-1)}{2}
     \]
   - **Case (ii):** If \( v_1 \geq \cdots \geq v_{k-1} > v_k \leq \cdots \leq v_n \), applying the claim to ants \( 1, 2, \ldots, k \) and also to ants \( n, n-1, \ldots, k \) by switching their order, the number of collisions is at most:
     \[
     \frac{k(k-1)}{2} + \frac{(n-k+1)(n-k)}{2} = \frac{n(n-1)}{2} - (k-1)(n-k)
     \]
     which is less than \( \frac{n(n-1)}{2} \).

6. **Constructing an Example:**
   Place ants at \( x = 1, 2, \ldots, n \) with positive initial velocities and speeds \( v_1 = \cdots = v_{n-1} = 1 \) and \( v_n = \varepsilon \) for some small \( \varepsilon \). For \( \varepsilon = 0 \), collisions occur such that ants \( m \) and \( m+1 \) collide exactly \( m \) times for \( m = 1, 2, \ldots, n-1 \). This yields:
   \[
   1 + 2 + \cdots + (n-1) = \frac{n(n-1)}{2}
   \]
   collisions in total. For sufficiently small \( \varepsilon > 0 \), the number of collisions remains \( \frac{n(n-1)}{2} \).

The final answer is \( \boxed{ \frac{n(n-1)}{2} } \)