The San Francisco 49ers trailed throughout the game until the final 18 seconds as the 49ers beat Dallas 24-21.