5. [5 points] A magician has a set of $16^{2}$ different cards. Each card has one side red and the other side blue; on each card, there is a natural number from 1 to 16 written on both sides. We will call a card a duplicate if the numbers on both sides of the card are the same. The magician wants to draw two cards such that at least one of them is a duplicate, and no number appears on both drawn cards simultaneously. In how many ways can he do this?
Answer: 3480.

Solution. Since the magician has a set of $16^{2}$ cards, there are all possible card variants (for each pair of numbers $(i ; j)$, where $1 \leqslant i \leqslant 16,1 \leqslant j \leqslant 16$ there will be a card with the number $i$ written on the red side and $j$ on the blue side). Let's consider two types of card sets.

1) Both cards are duplicates. The number of ways to choose a pair of cards is

$$
C_{16}^{2}=\frac{16(16-1)}{2}=120
$$

2) Only one of the cards is a duplicate. Then it can be chosen in 16 ways, after which the second card can be chosen in any way except for: (a) all duplicates (16 in total, including the already chosen card), (b) all cards that have the same number on the red side as the chosen duplicate (15 in addition to the chosen card), (c) all cards that have the same number on the blue side as the chosen duplicate (also 15). Thus, the second card can be chosen in $16^{2}-16-15-15=210$ ways. Therefore, in this case, there are $16 \cdot 210=3360$ ways to choose a pair of cards.

Thus, there are a total of $120+3360=3480$ ways.