#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
const int MOD = 998244353;
const int lim = 1e6 + 100;
long long int fact[lim + 10], inv_fact[lim + 10];
long long int fast_pow(long long int a, long long int exp,
                       long long int mod = MOD) {
  long long int ret = 1;
  while (exp) {
    if (exp & 1) {
      ret = (ret * a) % mod;
    }
    a = (a * a) % mod;
    exp >>= 1;
  }
  return ret;
}
long long int modInverse(int n, int mod) { return fast_pow(n, mod - 2, mod); }
long long int C(long long int n, long long int k) {
  if (n < k) return 0;
  if (n == k) return 1;
  long long int denom = (inv_fact[k] * inv_fact[n - k]) % MOD;
  return (fact[n] * denom) % MOD;
}
void pre() {
  fact[0] = 1;
  for (int i = 1; i <= lim; i++) fact[i] = (1LL * i * fact[i - 1]) % MOD;
  inv_fact[lim] = modInverse(fact[lim], MOD);
  for (int i = lim; i >= 0; i--)
    inv_fact[i - 1] = (1LL * i * inv_fact[i]) % MOD;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  pre();
  long long int ans = fast_pow(fast_pow(3, n) - 3, n);
  for (__typeof(n + 1) i = (1) - ((1) > (n + 1));
       i != (n + 1) - ((1) > (n + 1)); i += 1 - 2 * ((1) > (n + 1))) {
    if (i % 2 == 1) {
      ans -= C(n, i) * fast_pow(fast_pow(3, n - i) - 1, n) * 3;
      ans %= MOD;
      ans -= C(n, i) * (fast_pow(3, i) - 3) % MOD *
             fast_pow(fast_pow(3, n - i), n);
      ans %= MOD;
    } else {
      ans += C(n, i) * fast_pow(fast_pow(3, n - i) - 1, n) * 3;
      ans %= MOD;
      ans += C(n, i) * (fast_pow(3, i) - 3) % MOD *
             fast_pow(fast_pow(3, n - i), n);
      ans %= MOD;
    }
  }
  ans = (fast_pow(3, (long long int)n * n) - ans) % MOD;
  ans = (ans + MOD) % MOD;
  cout << ans << endl;
}
