title: Subsequences Size Three Array Whose Sum Divisible M 1
signature: subsequencesSizeThreeArrayWhoseSumDivisibleM1(a, n, m) {
source_py: def subsequences_size_three_array_whose_sum_divisible_m_1(A, N, M):
      ans = 0
      h = ([0] * M)
      for i in range(0, N):
          A[i] = (A[i] % M)
          h[A[i]] = (h[A[i]] + 1)
      for i in range(0, M):
          for j in range(i, M):
              rem = ((M - ((i + j) % M)) % M)
              if (rem < j):
                  continue
              if ((i == j) and (rem == j)):
                  ans = (ans + (((h[i] * (h[i] - 1)) * (h[i] - 2)) / 6))
              elif (i == j):
                  ans = (ans + (((h[i] * (h[i] - 1)) * h[rem]) / 2))
              elif (i == rem):
                  ans = (ans + (((h[i] * (h[i] - 1)) * h[j]) / 2))
              elif (rem == j):
                  ans = (ans + (((h[j] * (h[j] - 1)) * h[i]) / 2))
              else:
                  ans = (ans + ((h[i] * h[j]) * h[rem]))
          return ans
source_cpp: int subsequences_size_three_array_whose_sum_divisible_m_1(vector<int> A, int N, int M) {
    int ans = 0;
    int h [ M ] = {
      0 };
      for ( int i = 0;
      i < N;
      i ++ ) {
        A [ i ] = A [ i ] % M;
        h [ A [ i ] ] ++;
      }
      for ( int i = 0;
      i < M;
      i ++ ) {
        for ( int j = i;
        j < M;
        j ++ ) {
          int rem = ( M - ( i + j ) % M ) % M;
          if ( rem < j ) continue;
          if ( i == j && rem == j ) ans += h [ i ] * ( h [ i ] - 1 ) * ( h [ i ] - 2 ) / 6;
          else if ( i == j ) ans += h [ i ] * ( h [ i ] - 1 ) * h [ rem ] / 2;
          else if ( i == rem ) ans += h [ i ] * ( h [ i ] - 1 ) * h [ j ] / 2;
          else if ( rem == j ) ans += h [ j ] * ( h [ j ] - 1 ) * h [ i ] / 2;
          else ans = ans + h [ i ] * h [ j ] * h [ rem ];
        }
      }
      return ans;
    }