```python
def eulerian_num(n, m):
    """
    Calculates the Eulerian number A(n, m), which is the number of
    permutations of {1, ..., n} with exactly m ascents.
    """
    # Handle boundary conditions. A(n, m) is non-zero only for n > 0
    # and 0 <= m < n, with the special case A(0, 0) = 1.
    if n == 0 and m == 0:
        return 1
    if n <= 0 or m < 0 or m >= n:
        return 0

    # Use a dynamic programming approach to build a table of Eulerian numbers.
    # dp_table[i][j] will store the value of A(i, j).
    dp_table = [[0] * (m + 1) for _ in range(n + 1)]

    # Base case: A(i, 0) = 1 for all i >= 0.
    for i in range(n + 1):
        dp_table[i][0] = 1

    # Fill the table using the recurrence relation:
    # A(i, j) = (i - j) * A(i - 1, j - 1) + (j + 1) * A(i - 1, j)
    for i in range(2, n + 1):
        for j in range(1, m + 1):
            # The number of ascents 'j' must be less than the number of elements 'i'.
            if j < i:
                dp_table[i][j] = (i - j) * dp_table[i - 1][j - 1] + (j + 1) * dp_table[i - 1][j]
            # Otherwise, A(i, j) is 0, and the table is already initialized with zeros.

    return dp_table[n][m]

```