```python
def eulerian_num(n, m):
    """
    Calculates the Eulerian number A(n, m), which counts the number of
    permutations of {1, 2, ..., n} with exactly m ascents.
    """
    # Handle edge cases based on the definition of Eulerian numbers.
    # A(0, 0) = 1.
    if n == 0 and m == 0:
        return 1
    # A(n, m) = 0 if m >= n (for n>0) or m < 0 or n < 0.
    if m >= n or m < 0 or n <= 0:
        return 0

    # Use dynamic programming based on the recurrence relation:
    # A(n, m) = (n - m) * A(n - 1, m - 1) + (m + 1) * A(n - 1, m)
    
    # dp[i][j] will store A(i, j)
    dp = [[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[i][0] = 1

    # Fill the DP table.
    for i in range(1, n + 1):
        for j in range(1, m + 1):
            # The recurrence is valid for j < i.
            # For j >= i, A(i, j) is 0, which is the default value in our table.
            if j < i:
                dp[i][j] = (i - j) * dp[i - 1][j - 1] + (j + 1) * dp[i - 1][j]

    return dp[n][m]
```