Try the following formula:

=IF(AND($D2="",$E2=""),$F2,IF(COUNTIF($G$1:G$1,$E2)>0,0,IF(COUNTIF(H$1:$R$1,$D2)>0,0,$F2)))