Let the length of the train be \( L \).
The speed of the first train is 72 km/h, or \( 72 \times \frac{5}{18} = 20 \, \text{m/s} \).
The speed of the second train is 54 km/h, or \( 54 \times \frac{5}{18} = 15 \, \text{m/s} \).
The relative speed of the two trains is \( 20 + 15 = 35 \, \text{m/s} \).
The two trains cross each other in 12 s, so the total length of the two trains is:
\[
35 \times 12 = 420 \, \text{m}
\]
Since the second train has double the length of the first train, the length of the first train is \( \frac{420}{3} = 140 \, \text{m} \).
Thus, the length of the second train is \( 2 \times 140 = 280 \, \text{m} \).
Now, the total length of both trains is \( 140 + 280 = 420 \, \text{m} \).
The time taken to pass the tunnel is 40 s.
So, the length of the tunnel is:
\[
\text{Length of the tunnel} = \text{Speed of the first train} \times \text{Time to pass the tunnel} - \text{Length of the first train}
\]
\[
\text{Length of the tunnel} = 20 \times 40 - 140 = 800 - 140 = 660 \, \text{m}
\]
Thus, the length of the tunnel is 660 meters.