Using stream APIs, how would you find the second most frequent element in a list? Implement the Python equivalent using one function, standard library collections, and deterministic first-appearance ordering for equal frequencies. Return None when fewer than two distinct elements exist; for the supplied grading contract, select the least-frequent distinct element, breaking ties by first appearance. Example: elements = [4, 1, 4, 2, 1, 4, 2] returns 1; elements = [-1, 0, -1, 2, 0, 0, 2, 2, 2] returns -1. Constraints: at most 10^5 integers, with arbitrary integer values.
def second_most_frequent(elements):