Problem p02554 (C++) — buggy submission, verdict: Time Limit Exceeded

/*
بسم الله الرحمن الرحيم
اللہ کے نام سے شروع جو بڑا مہربان اور نہایت رحم والا ہے
Allah in the name of The Most Affectionate, the Merciful
अल्लाह के नाम से जो दयालु और कृपाशील है

                                                        |		MD
BAHAUDDIN aka Mujahid	| |			NIT JAMSHEDPUR | |
linkedin.com/in/bahau/		|
*/

// #include<bits/stdc++.h>
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;
#define fastio                                                                 \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0)
#define ll long long
#define ull unsigned ll
#define pb push_back
#define vi vector<int>
#define vll vector<ll>
#define vvi vector<vi>
#define vvll vector<vll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define all(c) c.begin(), c.end()
#define mp(x, y) make_pair(x, y)
#define mem(t, val) memset(t, val, sizeof(t));
#define f first
#define s second
#define FOR(i, k, n) for (int i = k; i < n; i++)
#define ROF(i, k, n) for (int i = k; i >= n; i--)
#define endl '\\n'
#define mod (1000 * 1000 * 1000 + 7)

ll poww(ll x, ll n) {
  ll ans = 1;
  // while(n>0)
  // {
  // 	if(n%2==0)
  // 	{
  // 		x=x*x%mod; n=n/2;
  // 	}
  // 	else{
  // 		ans=ans*x%mod; n--;
  // 	}
  // }
  // return ans%mod;
  for (int i = 1; i <= n; i++) {
    ans = ans * x % mod;
  }
  return ans;
}
ll f(ll n) {
  ll ten = poww(10, n) % mod;
  ll nin = poww(9, n) % mod;
  ll eig = poww(8, n) % mod;
  return ((((((ten - nin) % mod + mod) % mod - nin) % mod + mod) % mod + eig) %
              mod +
          mod) %
         mod;
}

int main() {
  fastio;

#ifndef ONLINE_JUDGE
  // input from input.txt
  freopen("input.txt", "r", stdin);
  // output to output.cpp
  freopen("output.cpp", "w", stdout);
#endif

  int t = 1; // cin>>t;
  while (t--) {
    ll n;
    cin >> n;

    cout << f(n);
  }
  return 0;
}


Error: TLE

Fixed submission:

/*
بسم الله الرحمن الرحيم
اللہ کے نام سے شروع جو بڑا مہربان اور نہایت رحم والا ہے
Allah in the name of The Most Affectionate, the Merciful
अल्लाह के नाम से जो दयालु और कृपाशील है

                                                        |		MD
BAHAUDDIN aka Mujahid	| |			NIT JAMSHEDPUR | |
linkedin.com/in/bahau/		|
*/

// #include<bits/stdc++.h>
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;
#define fastio                                                                 \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0)
#define ll long long
#define ull unsigned ll
#define pb push_back
#define vi vector<int>
#define vll vector<ll>
#define vvi vector<vi>
#define vvll vector<vll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define all(c) c.begin(), c.end()
#define mp(x, y) make_pair(x, y)
#define mem(t, val) memset(t, val, sizeof(t));
#define f first
#define s second
#define FOR(i, k, n) for (int i = k; i < n; i++)
#define ROF(i, k, n) for (int i = k; i >= n; i--)
#define endl '\\n'
#define mod (1000 * 1000 * 1000 + 7)

ll poww(ll n) {
  ll ans = 1;
  ll x = 10;
  while (n > 0) {
    if (n % 2 == 0) {
      x = x * x % mod;
      n /= 2;
    } else {
      ans = ans * x % mod;
      n--;
    }
  }
  return ans % mod;
}
ll fact(ll n) {
  // cout<<n<<endl;
  ll ans = 0;
  while (n) {
    n--;
    ans = (ans + n * 2) % mod;
  }
  // cout<<ans<<endl;
  return ans % mod;
}
ll f(ll n) {
  if (n < 2)
    return 0;
  ll ans = poww(n - 2);

  ans = ans * fact(n) % mod;
  return ans;
}

int main() {
  fastio;

#ifndef ONLINE_JUDGE
  // input from input.txt
  freopen("input.txt", "r", stdin);
  // output to output.cpp
  freopen("output.cpp", "w", stdout);
#endif

  int t = 1;
  cin >> t;
  while (t--) {
    ll n;
    cin >> n;

    cout << f(n) << endl;
  }
  return 0;
}