Determining size of FC layer after Conv layer in PyTorch
Hello and welcome to Stack Exchange!

The answer to your question is quite simple: you did not use the correct formula.

The formula you used is (assuming we are working with square inputs)

$$
W'=\frac{W-F+2P}{S}
$$

but the correct formula is

$$
W'=\frac{W-F+2P}{S}+1
$$

Now if we redo your calculations starting with $(1 \times 28 \times 28)$ inputs:

$$
W^{(1)}=28-4+1=25\\
W^{(2)}=\lfloor\frac{25-2}{2}+1\rfloor=12\\
W^{(3)}=12-3+1=10\\
W^{(4)}=\lfloor\frac{10-2}{2}+1\rfloor=5
$$

Considering that the second convolution layer has 16 output channels (or feature maps), you can indeed then calculate the number of inputs as $16\cdot5^2=400$.