Write a function to detect transaction anomalies within a highly active database log.
Each record is [timestamp, transaction_id, event, amount]. Events are BEGIN, WRITE, COMMIT, or ABORT; BEGIN.amount is the expected total and COMMIT.amount is the reported total. Return sorted transaction IDs whose lifecycle is invalid, whose committed amount does not equal the expected amount and sum of writes, or that remain unfinished. Preserve input order when processing records.
Signature: def detect_transaction_anomalies(log):
def detect_transaction_anomalies(log):