O(n ^ 2) approach with a basic optimization with comments
"""\n#define lld long long int\n                                     \n\nclass Solution {\npublic:\n    int numTeams(vector& v) {\n        int n = v.size();\n  
"""\n#define lld long long int\n                                     \n\nclass Solution {\npublic:\n    int numTeams(vector<int>& v) {\n        int n = v.size();\n            int  ans =0;\n    \n    // basically what iam trying to do is count the no of small values behind me and larger values ahead of me\n    // they will form a valid triplet\n        \n    // similarly elements which are larger behind me and smaller in front of me will form another valid triplet    \n        \n        \n    for(int i=0;i<n;i++){\n        lld behind1 = 0 , ahead1 = 0 , behind2 = 0 , ahead2 = 0;\n        \n        for(int j = 0 ; j < i ; j++){\n           if(v[i] > v[j])\n               behind1++;                    \n            else\n               ahead1++;       \n        }\n        \n         for(int j = i + 1 ; j < n ; j++){\n           if(v[i] > v[j])\n               behind2++;\n            else\n               ahead2++;       \n        }\n        \n         ans += (behind1 * ahead2) +  (ahead1 * behind2); // total pairs\n    \n    }\n    return ans;\n    }\n   \n};\n\n"""
[]