The environment = familiar place, familiar smells, where any baseline experiences of fed, sheltered, warm...it doesn't have to be a happy household for these things to also be true. We're talking about sensory input for an infant.
The role (of the father) = familiar smells, familiar sounds.
For the baby, this feels like one thing. The role is an extension of the environment.
When the role (of the father, in this case) becomes = unfamiliar smell in familiar sounds - even something that small - can cause discomfort. If you add in that the father is tense in any way? The whole situation becomes very uncomfortable. If this is repeated behavior, the baby will develop in one of two ways - either they will remain uncomfortable and uneasy around the caregiver that brings discomfort (in this case, the father) - or, they will reconcile discomfort with safety. They will make sense of it - because that's what very early learning is, it's attaching physical sensation to feelings.
Not that I'm in any way qualified talking about this, but wanting to add a behavioral ecologist's/biologist's angle to this.
Infants and toddlers are very very similar to social animals (think higher mammals). They may not yet understand abstract concepts and lack a theory of mind ("since a baby doesn’t know what’s happening and doesn’t even know it has a body"), hence they rely much more on those more basic instincts and subtle cues about their environment, which includes people.
Many animals are
extremely good at sensing even the most subtle cues of changes in their environment that they have previously learned is safe. (which is what makes service dogs service dogs, because they pick up on those internal teeny tiny changes of their handler; or you know how your pet always seems to just KNOW when it's time to go to the vet and will just disappear before you even pull out the carrier). And they will react to it. Keep in mind, 'reaction' doesn't necessarily have to be visibly changed behaviors. It can be as little as just a slightly elevated internal alertness. Animals also are very sensitive to the behaviors from others around them. Put an animal in a group of anxious animals and it will become anxious, too. A lot if not most of this is instinct and reflexes. Some of this is learned. But learning can happen really really really fast.
And babies are just like that. Many adults can still sense if something is "off", if a person, no matter how hard they try to hide it, is acting differently, if something has changed in their evironment etc. And babies are inherently so much better at that
because they still lack language and theory of minds, so they
must rely on instinct and reflexes for their survival.
To come back to one of your examples: (theoretical, because as has been pointed out, doctors don't actually do that)
A father putting his finger into his daughter's vagina will know it's wrong, even if he really tries to convince himself it's not. He may tense up a tiny bit. He may have a slightly elevated breathing. He may smell different. If he's getting off of it, there
will be a change in his body that can be sensed.
A doctor putting his finger into a baby's vagina is merely doing his job. He's doing it all. the. time. He will not act differently than during any other exam methods.