Initialize a result variable to store the total number of teams.
Iterate over the soldiers using a loop with index i.
For each soldier i, initialize four counters: lessLeft, greaterLeft, lessRight, and greaterRight.
Use two nested loops to count the number of soldiers with a lower and higher rating to the left and right of soldier i.
Update the result by adding the product of lessLeft and greaterRight (for ascending teams) and the product of greaterLeft and lessRight (for descending teams).
Return the result as the total number of valid teams.