N Log N + clean code
\n\n# Code\n\nimport java.util.PriorityQueue;\n\nclass Solution {\n\n    // Nested class representing a buy order\n    public class Buy {\n        int price;\n 
\n\n# Code\n```\nimport java.util.PriorityQueue;\n\nclass Solution {\n\n    // Nested class representing a buy order\n    public class Buy {\n        int price;\n        int amount;\n\n        public Buy(int p, int a) {\n            price = p;\n            amount = a;\n        }\n    }\n\n    // Nested class representing a sell order\n    public class Sell {\n        int price;\n        int amount;\n\n        public Sell(int p, int a) {\n            price = p;\n            amount = a;\n        }\n    }\n\n    // Priority queues for buy and sell orders\n    PriorityQueue<Buy> buy;\n    PriorityQueue<Sell> sell;\n\n    // Method to get the number of backlog orders\n    public int getNumberOfBacklogOrders(int[][] orders) {\n        // Initialize priority queues\n        buy = new PriorityQueue<>((a, b) -> b.price - a.price); // Max heap for buy orders\n        sell = new PriorityQueue<>((a, b) -> a.price - b.price); // Min heap for sell orders\n\n        int mod = 1000000007; // Modulus value\n        int ans = 0; // Accumulator for total orders\n\n        // Process each order\n        for (int[] o : orders) {\n            int amount = o[1];\n            int price = o[0];\n            int type = o[2];\n\n            if (type == 0) {\n                buying(amount, price);\n            } else {\n                selling(amount, price);\n            }\n        }\n\n        // Sum up remaining orders\n        while (!buy.isEmpty()) {\n            ans = (ans + buy.poll().amount) % mod;\n        }\n\n        while (!sell.isEmpty()) {\n            ans = (ans + sell.poll().amount) % mod;\n        }\n\n        return ans;\n    }\n\n    // Method to process buy orders\n    public void buying(int amount, int price) {\n        while (!sell.isEmpty() && amount > 0 && sell.peek().price <= price) {\n            Sell currSell = sell.poll();\n            int quantity = amount - Math.min(currSell.amount, amount);\n            if (quantity == 0) {\n                currSell.amount -= amount;\n                sell.offer(currSell);\n            }\n            amount = quantity;\n        }\n\n        if (amount != 0) {\n            buy.offer(new Buy(price, amount));\n        }\n    }\n\n    // Method to process sell orders\n    public void selling(int amount, int price) {\n        while (!buy.isEmpty() && amount > 0 && buy.peek().price >= price) {\n            Buy currBuy = buy.poll();\n            int quantity = amount - Math.min(currBuy.amount, amount);\n            if (quantity == 0) {\n                currBuy.amount -= amount;\n                buy.offer(currBuy);\n            }\n            amount = quantity;\n        }\n\n        if (amount != 0) {\n            sell.offer(new Sell(price, amount));\n        }\n    }\n}\n\n```
['Java']