Let $n \geq 3$ be an integer. John and Mary play the following game: First John labels the sides of a regular $n$-gon with the numbers $1, 2,\ldots, n$ in whatever order he wants, using each number exactly once. Then Mary divides this $n$-gon into triangles by drawing $n-3$ diagonals which do not intersect each other inside the $n$-gon. All these diagonals are labeled with number $1$. Into each of the triangles the product of the numbers on its sides is written. Let S be the sum of those $n - 2$ products.

Determine the value of $S$ if Mary wants the number $S$ to be as small as possible and John wants $S$ to be as large as possible and if they both make the best possible choices.
1. **Labeling the Sides:**
   John labels the sides of the regular $n$-gon with the numbers $1, 2, \ldots, n$ in some order. Let the labels be $a_1, a_2, \ldots, a_n$ in clockwise order.

2. **Triangulation:**
   Mary divides the $n$-gon into $n-2$ triangles by drawing $n-3$ non-intersecting diagonals. Each diagonal is labeled with the number $1$.

3. **Product Calculation:**
   For each triangle formed, the product of the numbers on its sides is written. Let the triangles be $\Delta_1, \Delta_2, \ldots, \Delta_{n-2}$, and let the sides of $\Delta_i$ be labeled with $a_{i1}, a_{i2}, a_{i3}$. The product for $\Delta_i$ is $a_{i1} \cdot a_{i2} \cdot a_{i3}$.

4. **Sum Calculation:**
   Let $S$ be the sum of the products of the sides of all triangles:
   \[
   S = \sum_{i=1}^{n-2} a_{i1} \cdot a_{i2} \cdot a_{i3}
   \]

5. **Objective:**
   John wants to maximize $S$, while Mary wants to minimize $S$. Both will make the best possible choices.

6. **Optimal Strategy for Mary:**
   Mary will try to minimize the sum $S$. To do this, she will aim to minimize the products of the sides of the triangles. Since the diagonals are labeled with $1$, the products will be minimized when the sides of the triangles are as small as possible.

7. **Optimal Strategy for John:**
   John will try to maximize $S$. He will aim to place the largest numbers on the sides of the triangles as much as possible.

8. **Analysis of the Products:**
   Consider the sum of the products of the sides of the triangles. Each side of the $n$-gon will appear in exactly two triangles. Therefore, the sum of the products can be written as:
   \[
   S = \sum_{i=1}^{n} a_i \cdot (a_{i-1} + a_{i+1})
   \]
   where $a_{i-1}$ and $a_{i+1}$ are the labels of the sides adjacent to $a_i$.

9. **Maximizing the Sum:**
   To maximize $S$, John should place the largest numbers in positions where they will be multiplied by the largest possible sums of adjacent numbers. 

10. **Minimizing the Sum:**
    Mary will try to minimize the sum by choosing a triangulation that minimizes the products of the sides of the triangles. However, since each side appears in exactly two triangles, her influence is limited.

11. **Conclusion:**
    Given that each side appears in exactly two triangles, the sum $S$ is determined by the sum of the products of the sides of the triangles. The optimal strategy for John is to place the numbers in such a way that the sum of the products is maximized.

    The sum of the products of the sides of the triangles is:
    \[
    S = \sum_{i=1}^{n} a_i \cdot (a_{i-1} + a_{i+1})
    \]
    Since each number $1, 2, \ldots, n$ appears exactly twice, the sum $S$ is maximized when the numbers are placed in such a way that the products are maximized.

The final answer is $\boxed{n(n+1)}$