#include <bits/stdc++.h>
using namespace std;
const int N = 1000000 + 5000;
const long long mod = 998244353ll;
long long pow_mod(long long a, long long b) {
  assert(b >= 0);
  long long res = 1;
  for (a %= mod; b; b >>= 1, a = a * a % mod)
    if (b & 1) res = res * a % mod;
  return res;
}
long long fact[N], inv[N];
inline long long C(long long n, long long m) {
  return ((fact[n] * inv[m]) % mod) * inv[n - m] % mod;
}
long long n;
long long power_3(long long a) { return pow_mod(3, a); }
int main() {
  fact[0] = fact[1] = 1;
  for (long long i = 1; i < N; ++i) fact[i] = fact[i - 1] * i % mod;
  inv[N - 1] = pow_mod(fact[N - 1], mod - 2);
  for (int i = N - 2; ~i; --i) inv[i] = inv[i + 1] * (i + 1) % mod;
  cin >> n;
  long long ans = 0;
  long long p = 1;
  for (int i = 1; i <= n; ++i) {
    long long x = p * C(n, i) * power_3(i) % mod * power_3(n * (n - i)) % mod;
    ans += x;
    ans %= mod;
    ans += x;
    ans %= mod;
    ans = (ans - p * C(n, i) * 3ll % mod * power_3(n * (n - i))) % mod;
    p *= -1;
  }
  p = 1;
  for (int i = 1; i <= n; ++i) {
    ans = (ans + p * C(n, i) * 3ll * pow_mod(power_3(n - i) - 1, n)) % mod;
    p *= -1;
  }
  ans += mod;
  ans %= mod;
  cout << ans << endl;
  return 0;
}
