Example 3. Write the numbers $1, 2, 3, \cdots$, 1986, 1987 on the blackboard. At each step, determine some numbers from those written and erase them, replacing them with the remainder of their sum divided by 7. After several steps, two numbers remain on the blackboard, one of which is 987. What is the second remaining number?
(13th All-Russian Mathematics Competition, 1987)
Solution: Clearly, at each step, the sum of all the numbers written down modulo 7 is preserved.

Let the remaining number be $x$, then $x+987$ is congruent to $1+2+\cdots+1987$ modulo 7.

Since $1+2+\cdots+1987=1987 \times 7 \times 142$ is divisible by 7, the remainder is 0, so $x+987$ is also divisible by 7. Since 987 is not the remainder when divided by 7, $x$ is the remainder of the operation when divided by 7, $0 \leqslant x \leqslant 6$. Since 987 is divisible by 7,
thus $x=0$.