crawled out of the ocean, squirmed on land, somehow survived, then formed male and female
Firstly, sexual reproduction (male and female) evolved way, way before that - in single celled organisms. It was inherited by all animals and plants (although some reverted to asexual reproduction), so must have evolved before either group arose - in their common ancestors.
It's not a sudden change either, there are a range of intermediate phases which don't quite count as sexual reproduction yet. e.g. There are still single celled organisms which reproduce asexually but also trade some genes with their neighbors in the process.
Okay, "Somehow survived"? You shouldn't be surprised, there are still things like mudskippers or lungfish that can do it now. They have adaptations for it, like strong shoulder bones and muscles for moving about, and lungs.
So did ancient primitive tetrapods, for which we have many transitional fossils showing the progression of these adaptations: Eusthenopteron, panderichthys, tiktaalik, icthyostega, acanthostega and many more.
"then formed male and female"
So they were already male and female, and living in a world with lots of male and female life. What was the next big adaptation relative to sexual reproduction?
The amniotic egg. Before this adaptation, those early tetrapods had to return to the water to lay eggs - the eggs weren't sealed in a case enough to survive on land. This greatly restricted how far they could spread, to areas nearby large water sources.
The amniotic egg allowed tetrapods to spread far inland, going generations with only enough water available to drink.