This part of the formula

M2-1+ROW(INDIRECT("1:"&TRUNC(N2-M2)+1))

builds up an array of all of the dates between the values in M2 and N2. The WEEKDAY is obtained for each of these dates, and compared against the value in O2, to give an array of TRUE/FALSE as to whether each date is that day of the week. The -- coerces those TRUE/FALSE values to 1/0 values, which SUM adds up to count the number of days in that date range that are the nominated days.

It can be written more simply as

=SUM(--(WEEKDAY(ROW(INDIRECT(N2&":"&M2)))=O2))

or if you want text day names, Sun, Mon, use

=SUM(--(TEXT(ROW(INDIRECT(N2&":"&M2)),"ddd")=O2))